在 Mvc2 中使用 jquery 在我的页面中加载部分视图时如何进行客户端验证
当我在类中使用 jquery 单击创建按钮时,我返回部分视图
public virtual PartialViewResult Create()
{
return PartialView("Create");
}
并加载页面中的视图,
function createVendor() {
jQuery.ajax({
type: 'GET',
url: 'Vendor/create',
success: function (result) {
$("#popup").html(result).fadeOut('slow').fadeIn('slow');
}
});
}
我使用 DataAnnotations 进行验证,引用 js 文件并
<% Html.EnableClientValidation(); %>
在开始表单之前添加添加。
问题是,当我第一次单击“保存”时数据错误,验证不起作用,但第二次单击“保存”时验证有效。
I am returning partial view
public virtual PartialViewResult Create()
{
return PartialView("Create");
}
and loading the view in my page when clicking create button with jquery
function createVendor() {
jQuery.ajax({
type: 'GET',
url: 'Vendor/create',
success: function (result) {
$("#popup").html(result).fadeOut('slow').fadeIn('slow');
}
});
}
in my class I am using DataAnnotations for validation, making reference for js files and add adding
<% Html.EnableClientValidation(); %>
before begin form.
the problem is that when I click save the first time with wrong data the validation not working but it is working the second time I click save.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
亲爱的大家,我在这里找到了答案,谢谢您:
http://weblogs.asp.net/imranbaloch/archive/2010/07/11/asp-net-mvc-client-side-validation-with-dynamic-contents .aspx
Dear all I found the answer here thanks for you :
http://weblogs.asp.net/imranbaloch/archive/2010/07/11/asp-net-mvc-client-side-validation-with-dynamic-contents.aspx
我对
EnableClientValidation()
的理解是,它检查模型并在需要的地方注入 html。因此,您可能需要在返回的部分视图中再次调用EnableClientValidation()
。My understanding of the
EnableClientValidation()
is that it examines the model and injects html wherever it needs to go. So you may need to callEnableClientValidation()
again in your partial view you return.这篇博文 是一个很好的资源。我知道问题并不是专门询问该插件,但当我在寻求帮助时到达这里时,我认为这可能对其他人有帮助。
This blogpost is a great resource if you're using the jQuery validation plugin. I know the question isn't asking about the plugin specifically but seeing as I arrived here while searching for help with it, I thought this might help someone else.