How to read html helper element name

Is there a way in mvc to read the name that will be assigned to control html?

For example, I use this code:

<div> @Html.LabelFor(x => x.Name) @Html.TextBoxFor(x => x.Name) @Html.ValidationMessageFor(x => x.Name) @Html.HiddenFor(x => x.Id) <div> I want to display here a TextBox name </div> </div> 

And I want to get the input name. This code is a fragment of a partial view. The item name looks like children [1] .Name

+6
c # asp.net-mvc asp.net-mvc-3 razor html-helper
source share
2 answers

@ Html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName ("Name") Or you can use the extension method for a generic HtmlHelper to use this with lambda expressions

 public static string GetFullHtmlFieldName<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression) { return htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(ExpressionHelper.GetExpressionText(expression)); } 

Usage will be (Html.GetFullHtmlFieldName(x => x.Name)

+13
source share

I assume this is only in newer versions of MVC, but you could just use:

 @Html.NameFor(x => x.Name) 

Documentation: http://msdn.microsoft.com/en-us/library/hh833703(v=vs.108).aspx

+8
source share

All Articles