开发者

Model prefix when using typed HTML helpers

开发者 https://www.devze.com 2022-12-25 03:24 出处:网络
The following generates an input element for a model field using a typed helper: Html.HiddenFor(m => m.FieldName)

The following generates an input element for a model field using a typed helper:

Html.HiddenFor(m => m.FieldName)

The generated field name is FieldName. How do I add a prefix to the name so that it will render as name="p开发者_如何学Pythonrefix.FieldName"?


You can set the prefix for the HtmlHelper with

htmlHelper.ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix = "foo";

So if you set Html.ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix = "Foo" before Html.HiddenFor(m => m.FormId) the resulting field name would become 'Foo.FormId'

I'd recommend writing an extension method for the HtmlHelper rather than handling this logic in the view. You might then want to use the modeltype's name as a prefix.


You can't. You could use editor templates instead. Brad Wilson has a series of blog posts describing them. Scott Gu also covers them in this post.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号