MVC 3 jQuery 对话框上的客户端验证

发布于 12-10 10:38 字数 1585 浏览 0 评论 0原文

我使用 jquery 对话框显示了很多表单,并且我希望在其上添加客户端验证。我读了一些例子,说 mvc 3 已经以某种方式支持 jquery 客户端验证,但我尝试包含必要的脚本,我的表单如下所示:

 @using (Html.BeginForm("CreateFood", "Home", FormMethod.Post, new { id = "formData" }))
       {
           @Html.ValidationSummary(false, "Please fix these errors.")

当我尝试提交表单而不填写必填字段时,我仍然不喜欢收到任何消息。谁能给我更多关于这个的想法/解释/例子?

这里真的需要帮助...谢谢...

更新(在我的对话框的脚本中添加)

 $createdialog.dialog("option", "buttons", {
        "Cancel": function () {
            //alert('Cancel');
            $createdialog.dialog('close');

        },
        "Submit": function () {

            var frm = $('#formData');
            $.ajax({
                url: '/Food/CreateFood',
                type: 'POST',
                data: frm.serialize(),
                success: $createdialog.dialog('close')
            });
        }
    });

一旦删除,打开对话框:

  // Once drop, open dialog to create food
    options.drop = function (event, ui) {
        // Get the ContainerImgName which food dropped at
        var cimg = $(this).attr('id');
        // Pass in ContainerImgName to retrieve respective ContainerID
        // Once success, set the container hidden field value in the FoodForm
        $.ajax({
            url: '/food/getcontainerid',
            type: 'GET',
            data: { cImg: cimg },
            success: function (result) { $('#containerID').val(result); }
        });
        clear();
        $.validator.unobtrusive.parse($createdialog);
        $createdialog.dialog('open');
    };

I am showing lots of form using jquery dialog and I wish to add in client side validation on it. I read through some examples, saying that mvc 3 already somehow support jquery client side validation, but I tried by including the necessary script, and my form like this:

 @using (Html.BeginForm("CreateFood", "Home", FormMethod.Post, new { id = "formData" }))
       {
           @Html.ValidationSummary(false, "Please fix these errors.")

When i try to submit my form without fill in the required field, I still dint get any message. Can anyone give me more idea / explanation / examples on this??

Really needs help here... Thanks...

UPDATE (add in the script for my dialog)

 $createdialog.dialog("option", "buttons", {
        "Cancel": function () {
            //alert('Cancel');
            $createdialog.dialog('close');

        },
        "Submit": function () {

            var frm = $('#formData');
            $.ajax({
                url: '/Food/CreateFood',
                type: 'POST',
                data: frm.serialize(),
                success: $createdialog.dialog('close')
            });
        }
    });

Once dropped, open dialog:

  // Once drop, open dialog to create food
    options.drop = function (event, ui) {
        // Get the ContainerImgName which food dropped at
        var cimg = $(this).attr('id');
        // Pass in ContainerImgName to retrieve respective ContainerID
        // Once success, set the container hidden field value in the FoodForm
        $.ajax({
            url: '/food/getcontainerid',
            type: 'GET',
            data: { cImg: cimg },
            success: function (result) { $('#containerID').val(result); }
        });
        clear();
        $.validator.unobtrusive.parse($createdialog);
        $createdialog.dialog('open');
    };

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

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

发布评论

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

评论(2

我遇到了同样的问题,解决方法:

 $(name).dialog({
            autoOpen: true,
            width: options.witdth,
            heigth: options.height,
            resizable: true,
            draggable: true,
            title: options.title,
            modal: true,
            open: function (event, ui) {
                // Enable validation for unobtrusive stuffs
                $(this).load(options.url, function () {
                    var $jQval = $.validator;
                    $jQval.unobtrusive.parse($(this));
                });

            }
        });

当然,您可以在对话框的关闭事件上添加验证,具体取决于您在做什么,在我的情况下,弹出窗口只是用于显示错误,因此我执行了验证关于内容的加载。 (此弹出窗口显示的是操作结果)

I've faced the same problem, solved with:

 $(name).dialog({
            autoOpen: true,
            width: options.witdth,
            heigth: options.height,
            resizable: true,
            draggable: true,
            title: options.title,
            modal: true,
            open: function (event, ui) {
                // Enable validation for unobtrusive stuffs
                $(this).load(options.url, function () {
                    var $jQval = $.validator;
                    $jQval.unobtrusive.parse($(this));
                });

            }
        });

of course you can add the validation on the close event of the dialog, depends on what you're doing, in my case the popup was just for displaying errors so I've performed validation on load of the content. (this pop up is displaying am Action result)

过潦2024-12-17 10:38:00

对于每个动态生成的表单,一旦将此内容注入到 DOM 中,您就需要手动运行验证器,如 这篇博文使用$.validator.unobtrusive.parse 函数。

For every dynamically generated form you need to manually run the validator once you inject this content into the DOM as shown in this blog post using the $.validator.unobtrusive.parse function.

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