ASP.NET MVC SelectList in ViewModel

后端 未结 1 1948
忘了有多久
忘了有多久 2021-02-14 09:54

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:31

    I've always had better luck using IEnumerable

    public class PersonCreateViewModel
    {
        public IEnumerable 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)
提交回复
热议问题