ASP.NET MVC 3 DropDownList help

Izi

Izi

Soldato
Joined
9 Dec 2007
Posts
2,718
T9kwE.png


Above is my model - I have created a select list (Categories) - how do I now display this select list in the view?

I am just starting out in MVC, but I can't believe I can't get this to work! :)
 
In the relevant action of your controller:

Code:
return View(new CategoryModel(category));


Then in the view:

Code:
<%= Html.DropDownList("ElementName", Model.Categories) %>


Almost forgot, in the declaration of your view, change the inherit to:

Code:
Inherits="System.Web.Mvc.ViewPage<System.Web.Mvc.SelectList>"
 
Last edited:
hmm <%= Html.DropDownList("ElementName", Model.Categories) %> this wasn't working until i changed the return type of SelectList to List<SelectListItem>

Anyway, got it working thanks for your help.

I am using Razor syntax and working without intellisense which didn't help.
 
Back
Top Bottom