开发者

How do I set the name attribute on a multiselect listbox generated using LIstBoxFor and MultiSelectList in Asp.net MVC 3?

开发者 https://www.devze.com 2023-03-04 05:50 出处:网络
Here is how I\'m currently calling the helper in my view (using Razor and C#): @Html.ListBoxFor(m => m.Measures, new MultiSelectList(Model.Measures, \"MsId\"),

Here is how I'm currently calling the helper in my view (using Razor and C#):

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

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

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

The source code generated is still:

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

Any help would be greatly appreciated.


The name is generated from the Expression<Func<TModel, TProperty>> parameter passed. The name of the property passed is used so in your case m => m.Measures the name Measures is used. This is to enable model binding to work when your form is posted to an action.

You can change the name value by change the property name to MeasuresId or if that's not possible you can use the non typed version

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


pass in:

new { name= "yourname"}

for your html attributes in the overload of ListBoxFor

0

精彩评论

暂无评论...
验证码 换一张
取 消