views:

42

answers:

3

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 item 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?

+1  A: 

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

XwipeoutX
+3  A: 

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

David Liddle
A: 
lakhlaniprashant.blogspot.com