[英]How can I get HTMLPREFIX inside the partial view i Razor
嗨,我需要在局部视图中添加Html Prefix。
我在这样的局部视图上附加了这样的前缀
Html.RenderPartial("Person", Model.Person, new ViewDataDictionary(Html.ViewDataContainer.ViewData)
{
TemplateInfo = new TemplateInfo { HtmlFieldPrefix = "DelegatePerson"}
});
现在,当我进入Person PartialView时,在这种情况下需要前缀。
@Html.TextBoxFor(model => model.CPR, new { @class = "numericTextbox", maxlength = 10, id = "CprField", onkeyup ="CprFieldEdited("[HTMLPrefix]_FieldName");" })
但是我不知道该怎么做。 我已经搜寻了很久了 我希望有人知道该怎么做或类似的方法。
编辑:
这意味着我应该能够告诉Javascript函数CprFieldEdited在这种情况下它应该搜索的html前缀是“ DelegatePerson”
编辑2:似乎我的意思不清楚,所以我将尝试提供更多代码。
主页:
//None relevant Code here
@Scripts.Render("~/bundles/person_search")
//None relevant Code here
<Body>
//None relevant Code here
Html.RenderPartial("Person", Model.Person, new ViewDataDictionary(Html.ViewDataContainer.ViewData)
{
TemplateInfo = new TemplateInfo { HtmlFieldPrefix = "DelegatePerson"}
});
//None relevant Code here
Html.RenderPartial("Person", Model.Person, new ViewDataDictionary(Html.ViewDataContainer.ViewData)
{
TemplateInfo = new TemplateInfo { HtmlFieldPrefix = "EmployerPerson"}
});
//None relevant Code here
</body>
在这种情况下,我在同一个页面上呈现了两个相同类型的Partials。 但是在它们内部,我需要告诉Javascript捆绑包,现在正在使用其中的一个,因为我不能使用ID,而正在将它们用于其他用途,我的想法是使用带前缀的Name属性,但是如下面的代码所示,我可以不要给javascript加上前缀以区分两个部分
@Html.TextBoxFor(model => model.CPR, new {id = "CprField", onkeyup ="CprFieldEdited("[FieldNameWithPartialPrefix]");" })
问题是我不能给CprFieldEdited(“ [FieldNameWithPartialPrefix]”); javascript“ [FieldNameWithPartialPrefix]”可以是“ DelegatePerson_Fieldname”或“ EmployerPerson”
您不需要使用HtmlFieldPrefix
传递前缀。 您可以使用ViewBag
(与ViewData
相似,但更推荐它)来传递它。
做就是了,
@Html.RenderPartial(
"Person",
Model.Person,
new ViewDataDictionary()
{
{ "Custom Prefix", "your_prefix" }
});
或者,您可以在部分广告中定义前缀,
@{
ViewBag.CustomPrefix = "your_prefix";
}
并以任何一种方式访问它,只需执行操作,
<a onkeyup="@ViewBag.CustomPrefix"> ... </a>
您可以使用ViewBag
传递dynamic
任何内容。 有关更多信息 。
((System.Web.Mvc.ViewDataDictionary)(ViewData)).TemplateInfo.HtmlFieldPrefix
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.