SelectListItem with data-attributes

后端 未结 5 2092
温柔的废话
温柔的废话 2020-11-29 01:59

Is there anyway to have a SelectList prepopulated on ViewModel with data-attributes ?

I want to do

@Html.DropdownListFor(m=> m.CityId, Model.Citi         


        
5条回答
  •  死守一世寂寞
    2020-11-29 03:02

    Here's the simple solution.

    Not everything has to be written with extension method in .NET code. One of the great things about MVC is it gives you easy access to construct your own HTML.

    With MVC4 you can get the id and name of the element on the expression tree with the helpers HTML.NameFor and HTML.IdFor

    
    

    Assuming Model.Cities is a collection of items that expose each of those properties. Then you should be all set.

    If you want reusability, consider making it an editor template for anything that is an Enumerable of Cities

提交回复
热议问题