开发者

How to read name of html helper element

开发者 https://www.devze.com 2023-02-21 17:05 出处:网络
Is there in mvc any opportunity to read a name which will be assigned to html control? For example I use this code:

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 inp开发者_如何学Gout name. This code is fragment of partial view. Name of element looks like children[1].Name


@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)


I'm guessing this is only in newer versions of MVC, but you could probably just use:

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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号