0

I have created a custom ViewModel and one of the fields is a SelectList/DropDownlist. I wanted to use EditorForModel to display the ViewModel. All the fields work except the SelectList. I have tried a few different things and nothing has come up. I saw on one post that EditorForModel was not "smart enough" to do a DropDownList and I was wondering if that is true. The EditorForModel is so much easier to use and less typing :)

Thanks!

UPDATE

I figured it out. What I ended up doing was using the UIHint("TemplateName") and in the EditorTemplates folder created a .ascx file that outputted was I expected.

4

1 回答 1

0

在您的视图模型中,您应该有一个IEnumerable<SelectListItem>

public IEnumerable<SelectListItem> Months { get; set; }

而在aspx中,需要如下绑定:

<%
    var htmlAttributes = new Dictionary<string, object> { { "data-autopostback", "true" } }; 
%>

<%:Html.DropDownList("Month", Model.Months, "-- All --", htmlAttributes)%>

您需要确保在将 Months 属性传递给视图之前填充它。

如果您需要代码来填充 Months 属性,请告诉我。

于 2010-08-24T06:58:21.403 回答