How to get input name, e.g. mvc html HiddenFor

I want to write an HtmlHelper extension method similar to HiddenFor. The difference is that I want to control what is included in the value attribute, as well as stick to the additional attribute. I decided that I could just take a look at the source code for MVC, being both open source and with everyone. But, if I'm not blind, it seems that strongly typed helper methods such as HiddenFor, TextBoxFor, etc., are not included in the source code.

The method signature uses the expression Expression>.

Given the expression, how can I get the same name attribute value as HiddenFor?

+4
source share
1 answer

But, if I'm not blind, it seems that a strongly typed helper methods such as HiddenFor, TextBoxFor, etc., are not included in the source code

Everything is included in the source code, just download it and see InputExtensions.cs .

So, to get the name:

 public static MvcHtmlString FooBarFor<TModel, TProperty>( this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression ) { var name = ExpressionHelper.GetExpressionText(expression); var fullHtmlFieldName = htmlHelper .ViewContext .ViewData .TemplateInfo .GetFullHtmlFieldName(name); // do something with the name ... } 

Metadata ModelMetadata = ModelMetadata.FromLambdaExpression (expression, htmlHelper.ViewData)

+4
source

All Articles