ASP.NET MVC 中 EditorFor() 的 Html 属性 [英] Html attributes for EditorFor() in ASP.NET MVC
问题描述
为什么我不能将 html 属性传递给 EditorFor()
?例如;
Why can't I pass in html attributes to EditorFor()
? eg;
<%= Html.EditorFor(model => model.Control.PeriodType,
new { disabled = "disabled", readonly = "readonly" }) %>
我不想使用元数据
更新:解决方案是从视图中调用它:
Update: The solution was to call this from the view :
<%=Html.EditorFor( model => model.Control.PeriodEndDate, new {Modifiable=model.Control.PeriodEndDateModifiable})%>
并在我的自定义 EditorTemplates/String.ascx 中使用 ViewData["Modifiable"]
在那里我有一些视图逻辑来确定是否向输入添加只读和/或禁用属性传递给 EditorFor()
的匿名对象是一个名为 additionalViewData
的参数,它的属性被传递给 ViewData
集合中的编辑器模板.>
and use ViewData["Modifiable"]
in my custom EditorTemplates/String.ascx where I have some view logic that determines whether to add readonly and/or disabled attributes to the input
The anonymous object passed into EditorFor()
is a parameter called additionalViewData
and its properties are passed to the editor template in the ViewData
collection.
推荐答案
EditorFor
处理元数据,所以如果你想添加 html 属性你 总是可以的.另一种选择是简单地编写自定义模板并使用 TextBoxFor
:
EditorFor
works with metadata, so if you want to add html attributes you could always do it. Another option is to simply write a custom template and use TextBoxFor
:
<%= Html.TextBoxFor(model => model.Control.PeriodType,
new { disabled = "disabled", @readonly = "readonly" }) %>
这篇关于ASP.NET MVC 中 EditorFor() 的 Html 属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!