We need to select a model reference from a view. To do this we need to add a dropdownlistfor. this is based on a selectlist containing database models.
passing selectlists by viewbag
this solves our problem, but we do not like using ViewBag.
public ActionResult Create()
{
ViewBag.SomeModels = new SelectList(context.SomeModel, "id", "modelDescription");
return View();
}
Our solution
We added a context to a viewmodel to do the same, but this is MVC anti-pattern.
below is a snippet of our code.
public class SomeModelViewModel : SomeModel
{
private SomeContext context = new SomeContext ();
public SelectList getSomeOtherModels()
{
return new SelectList(context.SomeOtherModel, "id", "modelDescription");
}
}
Does anyone have suggestions for a clean way to solve this problem?
SelectList? Can't you have the items for the select list in one Property, and the selected value in another? – Jeff Vanzella Feb 14 at 16:58