【问题标题】:How to get the HTML id generated by asp.net MVC EditorFor如何获取asp.net MVC EditorFor生成的HTML id
【发布时间】:2011-06-17 06:48:12
【问题描述】:

我使用 HTML Helpers 来呈现我的字段:

<%=Html.EditorFor(m => m.User.Surname)%>

输出会是这样的:

<input class="text-box single-line" id="User_Surname" 
          name="User.Surname" type="text" value="" />

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

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-2


    【解决方案1】:

    除非您创建自己的 User.Surname EditorTemplate 并传入一些 HTML 属性,否则您将无法使用 EditorFor 执行此操作

    但是你可以使用TextBoxFor 并设置id

    <%= Html.TextBoxFor(m => m.User.Surname, new { id = "myNewId" })%>
    

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

    【讨论】:

    • 添加 HTML 属性是最简单的方法。比编写自己的扩展方法容易得多
    【解决方案2】:

    我已经按照 Mac 的回答 here 的说明构建了自己的自定义扩展:

    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();
        }
    }
    

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

    <%@ Import Namespace="MvcApplication2" %>
    

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

    <%=Html.HtmlIdNameFor(m=>m.Customer.Name)%>
    

    【讨论】:

    • 值得注意的是,你需要一个'using System.Linq.Expressions;'为此工作。无论如何都是甜蜜的代码:)
    • 我认为您还需要替换 '[' & ']' (如果您正在引用数组元素)
    【解决方案3】:

    看到这个问题:get the generated clientid for a form field,这是我的答案:

    我使用这个助手:

    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)));
        }
    }
    

    像使用其他助手一样使用它:@Html.ClientIdFor(model=&gt;model.client.email)

    【讨论】:

    • 根据我的回答,这现在已通过 Html.IdFor() 内置到 ASP.NET 4 中
    • 非常感谢我需要它。 html.IdFor() 没有帮助我,因为我正在制作自定义助手,
    【解决方案4】:

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

    @Html.IdFor(m => m.User.Surname)
    

    【讨论】:

      猜你喜欢
      • 2011-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多