开发者

Pre-Selecting a value for a dropdownbox

开发者 https://www.devze.com 2023-01-10 02:49 出处:网络
As the title says really <td><%= Html.DropDownList(\"Weight\", Model.Weightddl.Select(x => new SelectListItem { Text = x.Name, Value = x.Id.ToString() }))%><开发者_Go百科;/td>

As the title says really

<td><%= Html.DropDownList("Weight", Model.Weightddl.Select(x => new SelectListItem { Text = x.Name, Value = x.Id.ToString() }))%><开发者_Go百科;/td>

Here is my line of code. I have the value Model.Weight which I want to have preselected on the load... How can I set it as the selected value?

thanks

Steve


Set the Selected property, like this:

<td><%= Html.DropDownList("Weight", 
             Model.Weightddl.Select(x => new SelectListItem { 
                 Text = x.Name, 
                 Value = x.Id.ToString(),
                 Selected = (x.Id == Model.Weight)
             }))%></td>
0

精彩评论

暂无评论...
验证码 换一张
取 消