I want to get dropdown list with values from table. I'm have model:
namespace MobileService.Models
{
public class Mobile
{
public int Id { get; set; }
public string Model { get; set;}
Public string EMEI { get; set ;}
public int MasterId { get; set; }
public List<Master> MasterList { get; set; }
}
public class Master
{
public int Id { get; set; }
public string Name { get; set; }
}
}
In controller I creat action for this model:
public ActionResult Create()
{
return View();
}
In view:
@model MobileService.Models.Mobile
@using (Html.BeginForm()) {
@Html.EditorFor(model => model.Model)
@Html.EditorFor(model => model.EMEI )
// And here i want to display a dropdown list with all available masters
<button type="submit" class="btn btn-primary">Creat</button>
}
But it always tell me that Model.MasterList is null . But why? In model i told to get list from another model (public List MasterList { get; set; }). Why is it null?
GOOGLE SEARCHcanYieldDropDownList.return View() // it's EMPTY. Therefore, your model is null, even though the View expectsMobile. Later, look forSelectListandDropDownList. SelectList can be tricky but once you have understood it, It's easy to createDropDonwList, or even betterDropFownListFor