我如何重用HtmlHelper.EditorFor中的表达式

时间:2013-11-19 07:27:42

标签: asp.net-mvc-3 razor

在我的cshtml文件中,我有以下调用

@foreach( var EducationPlan in Model.Fields) {
    @HtmlHelper.EditorFor(m => EducationPlan, "ViewFieldInputWithHidden")
}

在ViewFieldInputWithHidden.cshtml中我有

@HtmlHelper.TextBox(Model.MemberName, Model.Value, Model.HtmlAttributes)
@HtmlHelper.Hidden(Model.MemberName, Model.Value, new { id = Model.MemberName + "_Hidden" })

这给了我,删除了不重要的东西

<input id="EducationPlan_ResponsiblePerson" name="EducationPlan.ResponsiblePerson" type="text">
<input id="ResponsiblePerson_Hidden" name="EducationPlan.ResponsiblePerson" type="hidden">

当我写id时,有没有办法模拟/使用与TextBox用来获取“EducationPlan”字符串相同的值?或者更好的是,只有一种方法可以将“_Hidden”附加到id。

我找到了解决方案,我将其留给了其他遇到同样问题的人。

用以下代码替换隐藏有助于:

@Html.Hidden(Model.MemberName, Model.Value, new { id = Html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldId(Model.MemberName) + "_Hidden" })

因此,基本上TemplateInfo类以某种方式保存表达式。

0 个答案:

没有答案
相关问题