ASP.NET MVC provides a number of convenient HtmlHelpers for form elements which accept a Lamda Expression returning a Model property as their first argument. The key advantages of these HtmlHelpers is that they simplify value binding and guarantee compile-time type-safety for selected Model members.
Html.TextBoxFor(m => m.Request.Name) Html.LabelFor(m => m.Request.Name)
Sometimes it is necessary to know what the generated field Name or Id will be elsewhere in the view, but unfortunately, there is no public method available to return these values.
The following HtmlHelpers accept a Model property expression and return the same field Id and Name strings which would be produced by calling the standard form helpers.
public static string GetFullHtmlFieldName<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression) { return helper.ViewData.TemplateInfo.GetFullHtmlFieldName(expression); } public static string GetFullHtmlFieldId<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression) { return helper.ViewData.TemplateInfo.GetFullHtmlFieldId(expression); } public static string GetFullHtmlFieldName<TModel, TProperty>(this TemplateInfo templateInfo, Expression<Func<TModel, TProperty>> expression) { return templateInfo.GetFullHtmlFieldName(ExpressionHelper.GetExpressionText(expression)); } public static string GetFullHtmlFieldId<TModel, TProperty>(this TemplateInfo templateInfo, Expression<Func<TModel, TProperty>> expression) { return templateInfo.GetFullHtmlFieldId(ExpressionHelper.GetExpressionText(expression)); }