ASP.Net MVC 2.0: EditorFor setting name via attributes

三世轮回 提交于 2019-12-07 22:58:35

问题


Just wondering how do I mimic the following using attributes...

<%= Html.EditorFor(x => x.SportProgramIdList, "FormMultiSelectDropDownList", "SportProgramIds")%>

I know I can specify the template by using [UIHint("FormMultiSelectDropDownList")] but I am left with the problem with how to set the name...

Cheers Anthony


回答1:


I Guess you'll have to create your own CustomAttribute UINameAttribute. You could use the ModelMetadata to keep your attribute and then I'm not sure what would be the best way to get it, i guess you would have to overide the HtmlHelper.EditorFor Extension without the parameter and pass your attribute to the next.

I'm too lazy and to tired to try for a more complete answer.

look at :

Why You Don't Need ModelMetadata.Attributes

ASP.NET MVC 2 Templates, Part 2: ModelMetadata




回答2:


Use the objectAttributes argument

<%= Html.EditorFor(x => x.SportProgramIdList, "FormMultiSelectDropDownList", new { id="SportProgramIds" }) %>>

you can use this to set any of the html input attributes



来源:https://stackoverflow.com/questions/2232840/asp-net-mvc-2-0-editorfor-setting-name-via-attributes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!