在 EditorTemplate 中渲染字段名称(通过 EditorFor() 渲染)
我目前正在 ASP.NET MVC 中为网站构建管理后端。
在 ASP.NET MVC 应用程序中,我开始使用“EditorFor”辅助方法,如下所示:
<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>
在模型中,“Biography”字段已用以下内容修饰:[UIHelper("Html")]。
我有一个“Html”部分视图(在 Views/Shared/EditorTemplates 下):
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<System.XML.Linq.XElement>" %>
<textarea class="html">
<%= Model.ToString() %>
</textarea>
现在我想将“textarea”的“ID”属性设置为字段的名称,如下所示:
<textarea id="Biography" class="html">
...
</textarea>
但我不能查看一种在当前设置下做到这一点的方法。
我能想到的就是创建一个包含“Value”属性和“ControlID”属性的“Html”ViewModel。
但如果我基于该视图,而不是“System.XML.Linq.XElement”,它将不再与“EditorFor”辅助方法兼容,并且我必须手动完成所有操作。
有人遇到过类似的问题吗?
I'm currently building the Admin back-end for a website in ASP.NET MVC.
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>
In the model, the 'Biography' field has been decorated with: [UIHelper("Html")].
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>
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.
All I can think of is creating an 'Html' ViewModel that contains a 'Value' property and a 'ControlID' property.
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该能够从视图的 ViewData.TemplateInfo.HtmlFieldPrefix 属性中提取所需的 ID。像这样:
为了说明其工作原理,这里是 TemplateHelpers.cs(MVC2 Preview 1 源代码)中为编辑器模板控件初始化 ViewData 的位置:
在上面的调用中,初始化了“表达式”(调用堆栈的更上方)以及正在编辑的属性的名称。
顺便说一句,下面的 @Sperling 抓住了我最初错过的一个细节:如果您正在使用(或可能使用)非默认的
HtmlHelper.IdAttributeDotReplacement
,那么您将需要替换中的点>HtmlPrefix
属性与HtmlHelper.IdAttributeDotReplacement
。You should be able to pull out the desired ID from the ViewData.TemplateInfo.HtmlFieldPrefix property of the view. Like this:
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:
In the call above, "expression" is initialized (further up the call stack) with the name of the property being edited.
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 theHtmlPrefix
property withHtmlHelper.IdAttributeDotReplacement
.一直用它来生成id(带有模型前缀)。跳过 .Replace() 部分,如果
你想要 name 属性。
Have been using this to generate id(with model prefix). Skip the .Replace() part if
you want the name attribute.
在我们的例子中,我们必须将
Html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName
与ExpressionHelper.GetExpressionText
一起使用,在 razor 中的使用方式如下:
In our case we had to use
Html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName
withExpressionHelper.GetExpressionText
which in razor was used like this: