在 EditorTemplate 中渲染字段名称(通过 EditorFor() 渲染)

发布于 2024-08-06 03:00:42 字数 1128 浏览 10 评论 0原文

我目前正在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夏雨凉 2024-08-13 03:00:42

您应该能够从视图的 ViewData.TemplateInfo.HtmlFieldPrefix 属性中提取所需的 ID。像这样:

<%@ Control Language="C#"
      Inherits="System.Web.Mvc.ViewUserControl<System.XML.Linq.XElement>" %>
<textarea id="<%= ViewData.TemplateInfo.HtmlFieldPrefix %>" class="html">
    <%= Model.ToString() %>
</textarea>

为了说明其工作原理,这里是 TemplateHelpers.cs(MVC2 Preview 1 源代码)中为编辑器模板控件初始化 ViewData 的位置:

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),
    }
};

在上面的调用中,初始化了“表达式”(调用堆栈的更上方)以及正在编辑的属性的名称。

顺便说一句,下面的 @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:

<%@ Control Language="C#"
      Inherits="System.Web.Mvc.ViewUserControl<System.XML.Linq.XElement>" %>
<textarea id="<%= ViewData.TemplateInfo.HtmlFieldPrefix %>" class="html">
    <%= Model.ToString() %>
</textarea>

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),
    }
};

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 the HtmlPrefix property with HtmlHelper.IdAttributeDotReplacement.

做个ˇ局外人 2024-08-13 03:00:42

一直用它来生成id(带有模型前缀)。跳过 .Replace() 部分,如果
你想要 name 属性。

<%=Html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldId(String.Empty).Replace(".", HtmlHelper.IdAttributeDotReplacement) %>

Have been using this to generate id(with model prefix). Skip the .Replace() part if
you want the name attribute.

<%=Html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldId(String.Empty).Replace(".", HtmlHelper.IdAttributeDotReplacement) %>
蓝戈者 2024-08-13 03:00:42

在我们的例子中,我们必须将 Html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldNameExpressionHelper.GetExpressionText 一起使用,

在 razor 中的使用方式如下:

           // hiddenFor was caching the value of this html input, and the value alone, nothing else on the page!
            Expression<Func<Web.ViewModels.ApiSettingsViewModel, int>> expression = (m => m.OrgApiLoginCredentials[i].OrgApiLoginId); 
        }
        <input type="hidden" value="@Model.OrgApiLoginCredentials[i].OrgApiLoginId" name="@Html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(ExpressionHelper.GetExpressionText(expression))" class="data-org-api-login-id"/>

In our case we had to use Html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName with ExpressionHelper.GetExpressionText

which in razor was used like this:

           // hiddenFor was caching the value of this html input, and the value alone, nothing else on the page!
            Expression<Func<Web.ViewModels.ApiSettingsViewModel, int>> expression = (m => m.OrgApiLoginCredentials[i].OrgApiLoginId); 
        }
        <input type="hidden" value="@Model.OrgApiLoginCredentials[i].OrgApiLoginId" name="@Html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(ExpressionHelper.GetExpressionText(expression))" class="data-org-api-login-id"/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文