How to set the name attribute in a multi-village list generated using LIstBoxFor and MultiSelectList in Asp.net MVC 3?

This is how I call the helper in my view right now (using Razor and C #):

@Html.ListBoxFor(m => m.Measures, new MultiSelectList(Model.Measures, "MsId"), 
new Dictionary<string, object> { { "id", "measureId" } })

This works fine when setting the "id" in the form element, but when I try to do something like this:

@Html.ListBoxFor(m => m.Measures, new MultiSelectList(Model.Measures, "MsId"), 
new Dictionary<string, object> { { "id", "measureId" }, { "name", "measureId" } })

Generated Source Code:

<select id="measureId" multiple="multiple" name="Measures">

Any help would be greatly appreciated.

+3
source share
2 answers

The name is generated from the passed parameter Expression<Func<TModel, TProperty>>. The name of the property passed is used so that in your case the m => m.Measuresname is used Measures. This means that model binding works when your form is submitted to action.

, MeasuresId, ,

@Html.ListBox("measureId", new MultiSelectList(Model.Measures, "MsId"));
+3

:

new { name= "yourname"}

html ListBoxFor

+1

All Articles