如何获取asp.net MVC EditorFor生成的HTML id

Lef*_*tyX 62 asp.net-mvc asp.net-mvc-2

我使用HTML Helpers来渲染我的字段:

<%=Html.EditorFor(m => m.User.Surname)%>
Run Code Online (Sandbox Code Playgroud)

输出将是这样的:

<input class="text-box single-line" id="User_Surname" 
          name="User.Surname" type="text" value="" />
Run Code Online (Sandbox Code Playgroud)

帮助程序使用className +'.' èfieldName来构建id.
我需要将id传递给jQuery函数.有没有办法在没有硬编码的情况下拥有它?也许是一个可以使用ASP.NET MVC2约定的帮手?

bka*_*aid 138

ASP.NET MVC 4 内置了Html.IdFor(),可以返回:

@Html.IdFor(m => m.User.Surname)
Run Code Online (Sandbox Code Playgroud)


Joh*_*eer 43

看到这个问题:获取表单字段生成的clientid,这是我的答案:

我用这个助手:

public static partial class HtmlExtensions
{
    public static MvcHtmlString ClientIdFor<TModel, TProperty>(
        this HtmlHelper<TModel> htmlHelper, 
        Expression<Func<TModel, TProperty>> expression)
    {
        return MvcHtmlString.Create(
              htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldId(
                  ExpressionHelper.GetExpressionText(expression)));
    }
}
Run Code Online (Sandbox Code Playgroud)

像使用任何其他助手一样使用它: @Html.ClientIdFor(model=>model.client.email)

  • 根据我的回答,现在使用Html.IdFor()将其内置到ASP.NET 4中 (13认同)

Lef*_*tyX 9

我已经按照Mac的答案的指示在这里,我已经建立了自己的自定义扩展:

public static class HtmlHelperExtensions
{
    public static string HtmlIdNameFor<TModel, TValue>(
        this HtmlHelper<TModel> htmlHelper,
        System.Linq.Expressions.Expression<Func<TModel, TValue>> expression)
    {
        return (GetHtmlIdNameFor(expression));
    }

    private static string GetHtmlIdNameFor<TModel, TValue>(Expression<Func<TModel, TValue>> expression)
    {
        if (expression.Body.NodeType == ExpressionType.Call)
        {
            var methodCallExpression = (MethodCallExpression)expression.Body;
            string name = GetHtmlIdNameFor(methodCallExpression);
            return name.Substring(expression.Parameters[0].Name.Length + 1).Replace('.', '_');

        }
        return expression.Body.ToString().Substring(expression.Parameters[0].Name.Length + 1).Replace('.', '_');
    }

    private static string GetHtmlIdNameFor(MethodCallExpression expression)
    {
        var methodCallExpression = expression.Object as MethodCallExpression;
        if (methodCallExpression != null)
        {
            return GetHtmlIdNameFor(methodCallExpression);
        }
        return expression.Object.ToString();
    }
}
Run Code Online (Sandbox Code Playgroud)

我导入了我的应用程序的命名空间

<%@ Import Namespace="MvcApplication2" %>
Run Code Online (Sandbox Code Playgroud)

最后我可以像这样使用我的代码:

<%=Html.HtmlIdNameFor(m=>m.Customer.Name)%>
Run Code Online (Sandbox Code Playgroud)


hun*_*ter 7

除非你创建自己的User.Surname EditorTemplate,你传递了一些HTML属性,你将无法做到这一点 EditorFor

但是,您可以使用TextBoxFor和设置ID

<%= Html.TextBoxFor(m => m.User.Surname, new { id = "myNewId" })%>
Run Code Online (Sandbox Code Playgroud)

关于使用jQuery选择器检索此元素的主题,您可以实现此目的,而无需使用某些end-with选择器进行硬编码.使用它你可能仍然可以使用EditorFor,只需选择$("[id^='Surname]")`.如果你试图选择这个特定的元素,你真的没有办法在你的jQuery代码中硬编码SOMETHING.