在EditorTemplate渲染的字段名称(通过EditorFor呈现()) [英] Rendering the field name in an EditorTemplate (rendered through EditorFor())
问题描述
我目前正在建设管理后端在ASP.NET MVC网站。
I'm currently building the Admin back-end for a website in ASP.NET MVC.
在ASP.NET MVC应用程序中,我使用'EditorFor辅助方法,像这样开始:
In an ASP.NET MVC application, I've started using the 'EditorFor' helper method like so:
<div id="content-edit" class="data-form">
<p>
<%= Html.LabelFor(c => c.Title) %>
<%= Html.TextBoxFor(c => c.Title)%>
</p>
<p>
<%= Html.LabelFor(c => c.Biography) %>
<%= Html.EditorFor(c => c. Biography)%>
</p>
</div>
在该模型中,传现场已被装饰:[UIHelper(HTML)]
In the model, the 'Biography' field has been decorated with: [UIHelper("Html")].
我有一个'HTML'的局部视图(下查看/共享/ EditorTemplates):
I have an 'Html' partial view (under Views/Shared/EditorTemplates):
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<System.XML.Linq.XElement>" %>
<textarea class="html">
<%= Model.ToString() %>
</textarea>
现在我想有设置为字段的名称文本区域'的'ID'属性,像这样:
Now I'd like to have the 'ID' attribute of the 'textarea' set to the name of the field, like this:
<textarea id="Biography" class="html">
...
</textarea>
但我不能看到一个办法做到这一点与当前设置。
But I can't see a way to do that with the current set up.
所有我能想到的是创建一个包含值属性和控件ID属性的HTML视图模型。
All I can think of is creating an 'Html' ViewModel that contains a 'Value' property and a 'ControlID' property.
但是,如果我基于的视图关闭该,而不是'System.XML.Linq.XElement',这将不再是与EditorFor'helper方法兼容,我不得不手工做的一切。
But if I based the view off that, rather than 'System.XML.Linq.XElement', it would no longer be compatible with the 'EditorFor' helper method and I'd have to do everything manually.
有没有人遇到过类似问题吗?
Has anyone had a similar problem yet?
推荐答案
您应该能够从视图的ViewData.TemplateInfo.HtmlField preFIX财产拉出所需的ID。像这样的:
You should be able to pull out the desired ID from the ViewData.TemplateInfo.HtmlFieldPrefix property of the view. Like this:
<%@ Control Language="C#"
Inherits="System.Web.Mvc.ViewUserControl<System.XML.Linq.XElement>" %>
<textarea id="<%= ViewData.TemplateInfo.HtmlFieldPrefix %>" class="html">
<%= Model.ToString() %>
</textarea>
要说明为什么这个作品,这里是在TemplateHelpers.cs的地方ViewData的是一种编辑模板控件初始化(MVC2 preVIEW 1源):
To show why this works, here's the place in TemplateHelpers.cs (of MVC2 Preview 1 source) where ViewData is initialized for the Editor template control:
ViewDataDictionary viewData = new ViewDataDictionary(html.ViewDataContainer.ViewData) {
Model = modelValue,
TemplateInfo = new TemplateInfo {
FormattedModelValue = formattedModelValue,
ModelType = modelType,
HtmlFieldPrefix = html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldId(expression),
IsNullableValueType = (underlyingNullableType != null),
}
};
在上面的电话,前pression被初始化(进一步调用堆栈)与属性的名称进行编辑。
In the call above, "expression" is initialized (further up the call stack) with the name of the property being edited.
顺便说一句,下面@Sperling抓住一个细节我原来错过了:如果你使用(或可能使用)一个非默认的 HtmlHelper.IdAttributeDotReplacement
,那么你会想与 HtmlHelper.IdAttributeDotReplacement
来替换 HTML preFIX
属性点。
BTW, @Sperling below caught a detail I originally missed: if you're using (or might use) a non-default HtmlHelper.IdAttributeDotReplacement
, then you'll want to replace the dots in the HtmlPrefix
property with HtmlHelper.IdAttributeDotReplacement
.
这篇关于在EditorTemplate渲染的字段名称(通过EditorFor呈现())的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!