开发者

asp.net mvc2 dropdownlistfor

I want to create a dropdownlist in my asp.net MVC2 view and I am followin开发者_StackOverflowg code:

 foreach (var whiteout in Model)
        {
%>
<tr>
    <td>
        <%= whiteout.Field.NiceName%>
       <%  Html.DropDownListFor("anyname", Model); %>

<%
                }
            }
%>

but I am getting error that second parameter is not correct. Second parameter is a list. Here is how Model is declared at the top of partial view:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<EnviroTracker.Entities.Whiteout>>" %>

Please suggest how to fix this?


A DropDownListFor helper takes a SelectList as second argument and a lambda expression to a simple property as first:

<%= Html.DropDownListFor(
    x => x.SomeProperty, 
    new SelectList(Model.SomeList, "ValueProperty", "TextProperty")
) %>

If you want to use the weakly typed DropDownList helper you could manually specify the name of the property it will be bound to but the second argument should still be a SelectList:

<%= Html.DropDownList(
    "SomeProperty", 
    new SelectList(Model.SomeList, "ValueProperty", "TextProperty")
) %>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜