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
If you just want to display text options in dropdown then try to use list of 'selectlistitem' and then fill out this list with foreach loop like
after this add this list to your view field (MasterList for your case but change type to
List<SelectListItem>
) this will display the dropdown list and to get selected value you can use javascript and jquery with ajax call for further process.this will give you value and text for selected item.
In view to display dropdownlist you can try it like this.