Reputation: 109
I am populating a dropdownlist using this data:
return _lamb_database.Lambs().Select(lamb => new SelectListItem
{
Text = lamb.LambName,
Value = lamb.LambID.ToString()
}).ToList();
I am then passing this List to the view using a viewmodel. In the view I am showing the items in the viewmodel using this code:
@Html.DropDownListFor(m => m.SelectedLamb, Model.Facilities, "Select Lamb")
SelectedLamb is an integer which gives the unique identifer for the lamb in the database. I am trying to pass back the unique identifier for the lamb instead of the lamb's name. You can see in the above that I am trying to get this list to set SelectedLamb.
I am getting this error:
The ViewData item that has the key 'SelectedLamb' is of type 'System.Int32' but must be of type 'IEnumerable'.
Does anyone know how I can get this to work? I have spent so long on this now.
Upvotes: 1
Views: 84
Reputation: 14640
You need to set the Facilities
property again after submitting.
[HttpPost]
public ActionResult YourActionName(YourModel model)
{
model.Facilities = ..; // set again
return View(model);
}
Upvotes: 1