I am getting my head round mvc forms, and trying to bind a DropDownList
in mvc but get this error:
Value cannot be null.
Parameter name: items
On:
@Html.DropDownListFor(x => x.JobRoles, new SelectList(Model.JobRoles, "value", "text"))
My controller:
public ActionResult Create(ContactViewModel myViewData)
{
List<SelectListItem> JobRoles_list = new List<SelectListItem>();
JobRoles_list.Add(new SelectListItem
{
Text = "1",
Value = "1"
});
// ViewBag.JobRoles = JobRoles_list;
myViewData.JobRoles = JobRoles_list;
return View(myViewData);
}
My model:
public class ContactViewModel
{
public List<SelectListItem> JobRoles { get; set; }
}
My view:
@Html.DropDownListFor(x => x.JobRoles, new SelectList(Model.JobRoles, "value", "text"))