如何在 jquery 中引用/访问强类型 Html 助手?
我主要在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
达林的答案有效,但还有更好的方法。在 MVC Futures 程序集 中有一个
HtmlHelper
扩展方法。验证更新:
我从未使用过这个,并且我不确定它是否需要元素的 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.Update for validate:
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.这里有几个选项:
使用类选择器。
对于
.validate
你可以尝试这个:或者简单地覆盖 id:
You have a couple of options here:
Use a class selector.
For the
.validate
you could try this:or simply override the id: