开发者

How to set the option value through model

I need to have the value for the option in the following:

I have created my select as follows:

 <select id="fromSelectBox" multiple="multiple" >

          <% foreach (var it开发者_如何转开发em in Model.Projects) { %>
                   <option><%=Html.Encode(item.Text)%></option> 
                <%} 
            %>
            </select>

How do I set the value of the option using the value in the model which s item.ID?


Have a look at the HtmlHelper for drop down lists. The SelectList takes optional parameters for dataValueField and dataTextField.

<%= Html.DropDownList("fromSelectBox", 
    new SelectList(Model.Projects, "ID", "Text"))
%>

http://weblogs.asp.net/ashicmahtab/archive/2009/03/27/asp-net-mvc-html-dropdownlist-and-selected-value.aspx


Can't you just use <option value="<%=item.ID%>">?


Simple:


 <select id="fromSelectBox" multiple="multiple" >

      <% foreach (var item in Model.Projects) { %>
               <option value="<%=Html.Encode(Item.Text)%>"><%=Html.Encode(item.Text)%></option> 
            <%} 
        %>
        </select>

You can use any other property of item as well

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜