加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc-3 – MVC 3 jQuery对话框的客户端验证

发布时间:2020-12-16 09:23:08 所属栏目:asp.Net 来源:网络整理
导读:我使用 jquery对话框显示了很多表单,我希望在其上添加客户端验证.我通读了一些例子,说mvc 3已经以某种方式支持jquery客户端验证,但我尝试了包含必要的脚本,我的表单如下: @using (Html.BeginForm("CreateFood","Home",FormMethod.Post,new { id = "formData
我使用 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');
    };

解决方法

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

$(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));
                });

            }
        });

当然你可以在对话框的关闭事件上添加验证,取决于你正在做什么,在我的情况下,弹出窗口只是为了显示错误,所以我已经对内容的加载进行了验证. (这个弹出窗口显示的是Action结果)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读