MVC DropDownListFor

纵然是瞬间 提交于 2021-01-29 03:16:25

问题


I am new using MVC approach. How to using the DropDownListfor in the MVC? I need get the list from my master table and show the option into the Business Premise in the Business Profile object. This is my ViewModel

 public class SMEAppViewModel 

{        
    public WP2PBusinessProfile BuisinessProfile  { get; set; }        
    public IEnumerable<WP2PMasterDropDownList> MasterList { get; set; }       

}

In my controller, I already initialize the MasterList to the List

 var _MasterList = _context.WP2PMasterDropDownList.ToList();

And in my view, I able to display all the option in the List by using the following code

 @foreach (var karim in Model.MasterList.Where(c => c.Variable == "BusinessPremise"))
                    {

                        @Html.DisplayFor(modelitem => karim.Value) <br>                            

                    }

However, I using the following dropdownlistfor in my view, but my DropDownlistfor is not able to show my drop down option in my view.

 @Html.DropDownListFor(m => m.BuisinessProfile.BusinessPremise, new SelectList(Model.MasterList, "Id", "Value"), "Select value")
                                    @Html.LabelFor(m => m.BuisinessProfile.BusinessPremise, new { @class = "form-control" })

would anyone tell me any wrong in my code?

Thanks


回答1:


Try this in your view

@Html.DropDownListFor(m => m.Entity, new Project.Models.My_Class().My_Method, new { @class = "form-control" })

You cant get data from your DropDownListFor.

In the My_Method , You can use SelectList



来源:https://stackoverflow.com/questions/41297883/mvc-dropdownlistfor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!