如何在 jquery 中引用/访问强类型 Html 助手?

发布于 2024-09-17 01:11:51 字数 677 浏览 1 评论 0原文

我主要在 MVC1 中工作。我现在正在处理一些 MVC2 代码。我有带有强类型 Html 帮助程序的字段,例如:

        <div class="editor-field">
            <%: Html.TextBoxFor(model => model.Contract.client_name)%>
            <%: Html.ValidationMessageFor(model => model.Contract.client_name) %>
        </div>

How do I access this field in jquery?我以前会有:

$('#client_name').val();

或在 jquery .validate 中:

$('form').validate({
            rules: {
                client_name: "required"
            },
            messages: {
                client_name: "Client Name is required"
            }
});

I've mostly worked in MVC1. I'm now working with some MVC2 code. I have fields with Strongly Typed Html Helpers like:

        <div class="editor-field">
            <%: Html.TextBoxFor(model => model.Contract.client_name)%>
            <%: Html.ValidationMessageFor(model => model.Contract.client_name) %>
        </div>

How do I access this field in jquery? I previously would have:

$('#client_name').val();

or in jquery .validate:

$('form').validate({
            rules: {
                client_name: "required"
            },
            messages: {
                client_name: "Client Name is required"
            }
});

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

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

发布评论

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

评论(2

很酷又爱笑 2024-09-24 01:11:51

达林的答案有效,但还有更好的方法。在 MVC Futures 程序集 中有一个 HtmlHelper 扩展方法。

$('#<%: Html.IdFor(m => m.Contract.client_name) %>').val();

验证更新:

$('form').validate({
    rules: {
        '<%:Html.IdFor(m => m.Contract.client_name) %>': { required : true }
    },
    messages: {
        '<%:Html.IdFor(m => m.Contract.client_name) %>': { required: "Client Name is required" }
    }
});

我从未使用过这个,并且我不确定它是否需要元素的 id 或名称。如果是名称,请使用 Html.NameFor() 代替。

Darin's answer works, but there is better way to do it. in MVC Futures assembly there's HtmlHelper extension method for just that.

$('#<%: Html.IdFor(m => m.Contract.client_name) %>').val();

Update for validate:

$('form').validate({
    rules: {
        '<%:Html.IdFor(m => m.Contract.client_name) %>': { required : true }
    },
    messages: {
        '<%:Html.IdFor(m => m.Contract.client_name) %>': { required: "Client Name is required" }
    }
});

I never used this, and I'm not sure whether it wants id or name of the element. If it's name, use Html.NameFor() instead.

笔落惊风雨 2024-09-24 01:11:51

这里有几个选项:

使用类选择器。

对于 .validate 你可以尝试这个:

$('form').validate({
    rules: {
        'Contract.client_name': "required"
    },
    messages: {
        'Contract.client_name': "Client Name is required"
    }
});

或者简单地覆盖 id:

<%: Html.TextBoxFor(model => model.Contract.client_name, new { id = "client_name" })%>

You have a couple of options here:

Use a class selector.

For the .validate you could try this:

$('form').validate({
    rules: {
        'Contract.client_name': "required"
    },
    messages: {
        'Contract.client_name': "Client Name is required"
    }
});

or simply override the id:

<%: Html.TextBoxFor(model => model.Contract.client_name, new { id = "client_name" })%>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文