HTML属性为EditorFor()在ASP.NET MVC [英] 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" }) %>
我不希望使用的元数据
I don't want to use metadata
更新:解决的办法是从视图中调用这个:
Update: The solution was to call this from the view :
<%=Html.EditorFor( model => model.Control.PeriodEndDate, new {Modifiable=model.Control.PeriodEndDateModifiable})%>
和使用计算机[可修改]
在我的自定义EditorTemplates / String.ascx在那里我有一些决定是否要添加到只读和/或禁用属性视图逻辑输入
传递到 EditorFor()
匿名对象是一个名为 additionalViewData code>参数和属性都会被传递到编辑器模板中
的ViewData code>集合。
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属性您<一个href=\"http://aspadvice.com/blogs/kiran/archive/2009/11/29/Adding-html-attributes-support-for-Templates-_2D00_-ASP.Net-MVC-2.0-Beta_2D00_1.aspx\">could始终做到的。另一种选择是简单地写一个自定义模板,并使用 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" }) %>
这篇关于HTML属性为EditorFor()在ASP.NET MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!