I am just trying to populate a html.dropdown list using mvc2 in VS2008.
But the control is not displayed at all.
Here is my code
public ActionResult Index()
{
ViewData["Time"] = DateTime.Now.ToString();
var mdl = new List<SelectListItem>();
mdl.Add(new SelectListItem
{
Value = "1",
Text = "Module One"
});
mdl.Add(new SelectListItem
{
Value = "2",
Text = "Module Two"
});
ViewData["moduleList"] = new SelectList(mdl,"Value", "Text");
return View("MainMenu");
}
and here is the markup
<div>
<%Html.DropDownList("moduleList", (IEnumerable<SelectListItem>)ViewData["moduleList"]); %>
</div>
Where did i go wrong ?
You are best putting that stuff in your model so for example
in the controller
}
Now create the model
your page will look like this on a test site
Now an explanation, you have created a model for the view and this model is returned to the page by the controller the page is inheriting from a ViewPage which takes the generic argument of the model that was supplied to it by the controller
The markup is saying “give me a html drop down and mark the selected item as the first selected, the items come from the model (which is what your controller supplied it).
In the real world the data would come from your data layer and not directly in the controller (I like as little code in the controller as possible)
edit:
You have a typo for your example try this