ASP.NET MVC 2 Templates

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

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

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

But it always by default selects the first selected item in the list ... any ideas?

+3
source share
1 answer

I would think you need to create a viewdata or create a viewmodel to include a select list passing in a drop down list. For example, in the action of your controller, you should do 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)}

, , html.editorfor :  Html.Editor( "CountryLis", "CountryDropDown" )

. .

0

Source: https://habr.com/ru/post/1721468/


All Articles