Q:

Mark prepopulated select options as selected in asp.net razor

I am trying to do select an option in a select element. I tried the following code. But it does not work.
 <select>
    <option value="1" @(ViewBag.dt.Rows[0][1].ToString())=="1"? selected>A</option>
    <option value="2" @(ViewBag.dt.Rows[0][1].ToString())=="2"? selected>B</option>
    <option value="3" @(ViewBag.dt.Rows[0][1].ToString())=="3"? selected>C</option>
 </select>

Any help?
Thank you.

A:

Your conditional operators appear incomplete (invalid code).
Did you mean:
<option value="1" @(ViewBag.dt.Rows[0][1].ToString() == "1" ? "selected" : "")>A</option>

I would normally suggest switching to @Html.RadioButtonFor but that will not handle a two-dimensional array references. Are you display multiple items in a loop? You may be able to simplify the whole thing  (please show the rest of your page/code).

