ASP.NET MVC SelectList in ViewModel

后端 未结 1 979
小鲜肉
小鲜肉 2021-02-14 10:11

I\'m working in ASP.NET MVC 5 (but this most likely applies to previous versions also). Best way to ask this question is to show you the code:

Here is the View Model:

相关标签:
1条回答
  • 2021-02-14 10:40

    I've always had better luck using IEnumerable

    public class PersonCreateViewModel
    {
        public IEnumerable<SelectListItem> cities {get; set;}
        public int CityId { get; set; }
        public String Name { get; set; }
        public String Address { get; set; }
    }
    

    Also, you are going to need a property on the view model to capture the selected value like CityId.

    Then you can use:

    Html.DropDownListFor(m => m.CityId, Model.cities)
    
    0 讨论(0)
提交回复
热议问题