Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read name of html helper element

Is there in mvc any opportunity to read a name which will be assigned to html control?

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 a name of input name. This code is fragment of partial view. Name of element looks like children[1].Name

like image 348
nosbor Avatar asked Apr 01 '11 14:04

nosbor


People also ask

What are HTML helper tags?

Tag helper is a new feature in ASP.net MVC. It enables server-side code to create and render HTML elements in Razor View. It is feature of Razor View engine. They are the C# classes which participate in view generation by creating the HTML elements.

Which statements describe HTML helper objects?

The HtmlHelper class renders HTML controls in the razor view. It binds the model object to HTML controls to display the value of model properties into those controls and also assigns the value of the controls to the model properties while submitting a web form.

Which option is a built in HTML helper?

These helpers are used to render the most common types of HTML elements such as HTML text boxes, checkboxes and so on. To create a HTML form, we can use the BeginForm() and EndForm() extension methods of the HTML helper class.

What are HTML helpers in MVC ?- 5 marks?

What is HTML Helper in ASP.NET MVC 5? HTML Helpers are methods that return a string. Helper class can create HTML controls programmatically. HTML Helpers are used in View to render HTML content.


1 Answers

@Html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName("Name") Or you can use extension method for 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));
}

The use would be (Html.GetFullHtmlFieldName(x => x.Name)

like image 185
archil Avatar answered Sep 21 '22 19:09

archil