Populate DropDownList using MVC 4 & Entity Framework

前端 未结 4 743
礼貌的吻别
礼貌的吻别 2020-12-16 22:04

I\'m developing MVC4 & Entity Framework Application.I wanted to populate DropDownList,I wanted to bind Category List to Dodropdown list

IRepository Code

4条回答
  •  眼角桃花
    2020-12-16 22:11

    Using the ViewBag (as some have suggested in other answers/comments) to get data from your controller to view is generally seen as a code smell.

    Your ViewModel should ideally contain all of the data you need for your view. So use your controller to populate this data on a property of your ViewModel:

    SelectList ProductTypes { get; set; }
    

    Then bind your dropdown to this value

    @Html.DropDownListFor(m => m.ProductType, Model.ProductTypes)
    

    You can find this same answer given on this post.

提交回复
热议问题