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="prefix.FieldName"?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can set the prefix for the HtmlHelper with
So if you set
Html.ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix = "Foo"beforeHtml.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.