在 Mvc2 中使用 jquery 在我的页面中加载部分视图时如何进行客户端验证

发布于 2024-09-08 11:10:58 字数 595 浏览 0 评论 0原文

当我在类中使用 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 技术交流群。

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

发布评论

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

评论(3

逆蝶 2024-09-15 11:10:58

我对 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 call EnableClientValidation() again in your partial view you return.

演多会厌 2024-09-15 11:10:58

这篇博文 是一个很好的资源。我知道问题并不是专门询问该插件,但当我在寻求帮助时到达这里时,我认为这可能对其他人有帮助。

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.

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