ASP.NET MVC3, why does dropdownlist rely on viewbag even in a strongly typed view?

The reason you need a viewbag is because your Person instance, to which your view binds, does not have the data for all possible departments, only the one department it belongs to. What happens in this line: ViewBag. Department_Id = new SelectList(db.

Deparments, "Id", "DepartmentName") Is that all Departments from the DB are added to the Select List (for your DropDown to bind to) with Id as key asn DepartmentName as value. After your view is bound to the Person, the dropdown will show the appropriate Department.

The reason you need a viewbag is because your Person instance, to which your view binds, does not have the data for all possible departments, only the one department it belongs to. What happens in this line: ViewBag. Department_Id = new SelectList(db.

Deparments, "Id", "DepartmentName"); Is that all Departments from the DB are added to the Select List (for your DropDown to bind to) with Id as key asn DepartmentName as value. After your view is bound to the Person, the dropdown will show the appropriate Department.

Maybe I'm just looking for too much "magic" in the MVC framework. – Rick Aug 16 at 8:59 @Rick: I guess you do. The view only binds to the single Department defined by your Person.

You use the viewbag to transfer the full list of departments. By the way, the ViewBag is simply a wrapper around the ViewData (from the other answer) allowing you to create dynamic properties. – Edwin de Koning Aug 16 at 9:27.

If you are using strongly typed views, you should have a strongly typed model that you pass to the view: public class ViewData { public IList Departments {get; set;} } Then in you controller you have: public ActionResult Create() { ViewData model = new ViewData(); model. Departments = db. Deparments; return View(model); } The last step is to create the select list and drop down on the view: @model ViewData @SelectList list = new SelectList(Model.

Deparments, "Id", "DepartmentName"); @Html. DropDownList("Department_Id", list); Hope this makes sense.

– Rick Aug 16 at 8:53 Yes, I generally put all the view models in the models folder in the solution. Generally I name them {viewName}ViewData.Cs – Steve Aug 16 at 8:54.

Now I generate my Controller and Views. And in Person\Create. As I understand it, the DropDownList Html helper is using the ViewBag to create my drop down list.

However, FirstName and LastName seem to have their input fields created without relying on the ViewBag - so in my View, I can have compile time checking on my FirstName and LastName fields because my view is strongly typed, but not on my DropDownList. Is there a reason why the DropDownList for department not strongly typed or am I doing something wrong and there is a way to make it strongly typed?

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions