问题
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