开发者

ASP.NET MVC 2 Editor Templates

The following link explains editor templates: http://weblogs.asp.net/scottgu/archive/2009/07/31/asp-net-mvc-v2-preview-1-released.aspx

What I want to know is if I have a editor template for a drop down, how is the initial value set?

I have a drop down and I use the Html.EditorFor(c => c.Country, "CountryDropDown")

But it always 开发者_Go百科defaults to the first selected item in the list... any ideas?


I would think, you need to create a viewdata, or create a viewmodel to include the select list passing to the dropdown list. For example, in your controller action, you should do sth like this:

    //get your item for editing here i.e named itemToEdit
    //get your country collection here

      ArrayList countryList=New ArrayList;
       foreach (Country c In YourCountryCollection)
{          countryList.Add(New With {.Item = c.CountryName, .value = c.CountryID})
}
    Viewdata("CountryList")=New SelectList(countryList, "Value", "Item", itemToEdit.countryID)}

Now in your view, instead of using html.editorfor, you should uuse the following: Html.Editor("CountryLis", "CountryDropDown")

This should set your dropdownlist with the selected value. Hope this help.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜