.net – ASP.Net MVC framework and databinding


I am having some trouble grasping some concepts behind the MVC framework. I am doing a very simple application which categorizes products.

The Creation screen will simply use a dropdown list showing the list of categories, the name of the product and submit.

On a normal .Net app, I would databind a server dropdownlist in the Page_Load, but in a MVC app, what is the best way to retrieve my categories from the database and add them to the dropdown list?

(Sorry, my question is extremely noobish but unfortunately resources are spare on MVC, and examples are often broken due to early changes)

Best Solution

I'm not sure if I totally grasp, but if your page is displaying a single product and the only user input is just to select a category from a drop down list, I can sorta help (but I'm a noob too!).

Referencing this page:


You are going to want to create a SelectList in your controller for Categories (probably referencing an ID, and displaying a Name). You then add this SelectList to your ViewData. You could alternatively set this as part of your ViewData.Model and reference that from your View.

In your view you use the HtmlHelper for DropDownList that takes a SelectList as a parameter.

That link above should convey it much better, this is just a quick summary. That link is for preview 3, but I think it should all still apply.