【问题标题】:ASP.Net MVC 2.0: EditorFor setting name via attributesASP.Net MVC 2.0:EditorFor 通过属性设置名称
【发布时间】:2011-01-15 00:38:12
【问题描述】:

只是想知道如何使用属性来模仿以下内容...

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

我知道我可以使用 [UIHint("FormMultiSelectDropDownList")] 来指定模板,但我遇到了如何设置名称的问题......

干杯 安东尼

【问题讨论】:

    标签: c# .net asp.net-mvc-2 attributes editorfor


    【解决方案1】:

    我猜你必须创建自己的 CustomAttribute UINameAttribute。 您可以使用ModelMetadata 来保留您的属性,然后我不确定获得它的最佳方法是什么,我想您必须在没有参数的情况下覆盖HtmlHelper.EditorFor 扩展并将您的属性传递给下一个.

    我太懒了,太累了,无法尝试更完整的答案。

    看看:

    Why You Don't Need ModelMetadata.Attributes

    ASP.NET MVC 2 Templates, Part 2: ModelMetadata

    【讨论】:

      【解决方案2】:

      使用 objectAttributes 参数

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

      您可以使用它来设置任何 html 输入属性

      【讨论】:

      • 此约定不适用于 EditorFor 或 DisplayFor,因为您没有传入匿名 HTML 属性对象,它是匿名 ViewData 添加对象。在这里添加一些东西仍然需要在自定义编辑器/显示模板上使用它。这方面的一个例子是在answer to a similar question 中完成的。
      猜你喜欢
      • 2011-04-13
      • 1970-01-01
      • 2023-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-21
      • 1970-01-01
      相关资源
      最近更新 更多