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

发布于 2024-10-15 02:03:31 字数 392 浏览 1 评论 0原文

我使用 HTML 帮助器来呈现我的字段:

<%=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 约定的助手?

I use the HTML Helpers to render my fields:

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

and the output would be something like this:

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

The helper uses the className + '.' è fieldName to build the id.
I need to pass the id to a jQuery function. Is there a way to have it without hardcoding it?
Maybe an helper which can use the ASP.NET MVC2 conventions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

早茶月光 2024-10-22 02:03:31

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

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

ASP.NET MVC 4 has Html.IdFor() built in that can return this:

@Html.IdFor(m => m.User.Surname)
饮湿 2024-10-22 02:03:31

请参阅此问题:获取表单字段生成的 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)));
    }
}

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

See this question: get the generated clientid for a form field, this is my answer:

I use this helper:

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

Use it just as you would any other helper: @Html.ClientIdFor(model=>model.client.email)

吃颗糖壮壮胆 2024-10-22 02:03:31

我已按照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();
    }
}

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

<%@ Import Namespace="MvcApplication2" %>

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

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

I've followed the instructions of Mac's answer here and I've built my own custom extension:

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

I've imported my application's namespace

<%@ Import Namespace="MvcApplication2" %>

and finally I can use my code like this:

<%=Html.HtmlIdNameFor(m=>m.Customer.Name)%>
沙与沫 2024-10-22 02:03:31

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

,但是您可以使用 TextBoxFor 并设置 id

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

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

unless you create your own User.Surname EditorTemplate which you pass in some HTML attributes you won't be able to do this with EditorFor

However you can use TextBoxFor and set the id

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

On the topic of retrieving this element with a jQuery selector you can achieve this without having to hardcode by using some of the ends-with selector. Using that you could probably still use EditorFor and just select it with $("[id^='Surname]")`. If you're trying to select this specific element there's really no way to NOT hardcode SOMETHING in your jQuery code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文