ASP.NET MVC + Populate dropdownlist

前端 未结 2 773
无人共我
无人共我 2020-12-15 10:29

In my viewModel I have:

public class PersonViewModel
{
    public Person Person { get; set; }
    public int SelectRegionId { get; set; }
    public IEnumera         


        
相关标签:
2条回答
  • 2020-12-15 11:06

    You are creating SelectList instance twice. Get rid of one of them:

    @Html.DropDownListFor(model => model.SelectRegionId, Model.Regions, "Choose... ")
    
    0 讨论(0)
  • 2020-12-15 11:18

    Your ViewModel has a property of type 'IEnumerable', but SelectList does not satisfy that type. Change your code like this:

    public class PersonViewModel
    {
        public Person Person { get; set; }
        public int SelectRegionId { get; set; }
        public SelectList Regions { get; set; }
    }
    

    View:

    <div class="form-group">
         @Html.LabelFor(model => model.Person.Address.Region)
         @Html.DropDownListFor(model => model.SelectRegionId, Model.Regions, "Choose... ")
     </div>
    
    0 讨论(0)
提交回复
热议问题