Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between DropDownlist or DropDownListFor Html helper

It seems weird that I couldn't find an explanation of the difference between those two helpers, so I would assume that is something obvious but I missed.

Basically I am trying to decide which one I should use for my case, with the following simple Model:

public class Booking     {         public int ID { get; set; }         public Room Room { get; set; }         public DateTime StartTime { get; set; }         public DateTime EndTime { get; set; }         public ICollection<Equipment> Equipments { get; set; }         public string Who { get; set; }     } 

and I want display a simple Room DropDownlist for Adding and Editing Booking record.

After doing a lots of Google around, it seems that I probably need a DropDopwListFor, but not sure why and how?

like image 349
Paul L Avatar asked Nov 18 '11 12:11

Paul L


People also ask

What is HTML DropDownListFor?

The Html. DropDownListFor<TModel,TProperty> extension method is a strongly typed extension method generates <select> element for the property specified using a lambda expression. Visit docs.microsoft.com to know all the overloads of DropDownListFor method.

How do I select multiple options from a DropDownList in MVC?

DropDownListFor(m => m. location_code, Model. location_type, new { @class = "form-control", @multiple = "multiple" }). location_code is an List<int> and location_type is List<SelectListItem> populated with data.


2 Answers

Take the following two examples:

@Html.DropDownListFor(     x => x.EquipmentId,      new SelectList(Model.Equipments, "Id", "Text") ) 

and:

@Html.DropDownList(     "EquipmentId",      new SelectList(Model.Equipments, "Id", "Text") ) 

It is obvious that with the second example the name of the property you are binding the dropdown to is hardcoded as a magic string. This means that if you decide to refactor your model and rename this property Tooling support that you might be using has no way of detecting this change and automatically modifying the magic string you hardcoded in potentially many views. So you will have to manually search & replace everywhere this weakly typed helper is used.

With the first example on the other hand we are using a strongly typed lambda expression tied to the given model property so tools are able to automatically rename it everywhere it is used if you decide to refactor your code. Also if you decide to precompile your views you will get a compiler time error immediately pointing to the view that needs to be fixed. With the second example you (ideally) or users of your site (worst case scenario) will get a runtime error when they visit this particular view.

Strongly typed helpers were first introduced in ASP.NET MVC 2 and the last time I used a weakly typed helper was in an ASP.NET MVC 1 application long time ago.

like image 156
Darin Dimitrov Avatar answered Oct 12 '22 14:10

Darin Dimitrov


DropDownListFor will automatically select the selected value by using the specified property:

// Will select the item in model.Equipments that matches Model.EquipmentId @Html.DropdownListFor(m => m.EquipmentId, Model.Equipments);  

Another comment:

Don't have ICollection<Equipment> Equipments in your view model. You should have a property that returns an IEnumerable<SelectListItem>.

like image 42
jgauffin Avatar answered Oct 12 '22 12:10

jgauffin