必填字段验证在 JQuery Popup MVC 4 中不起作用 [英] Required field validations not working in JQuery Popup MVC 4

查看:28
本文介绍了必填字段验证在 JQuery Popup MVC 4 中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 JQuery 弹出窗口,我想在其上放置必需的字段验证,为此我在模型中设置了必需的属性,并在视图中为它们设置了验证消息,但必需的字段验证不适用于弹出窗口.必填字段验证在 JQuery 弹出窗口以外的表单上工作正常......请指导我我应该怎么做来解决这个问题......以下是我的代码.

I have JQuery popups and i want to put required field validations on it and for this i have set required attributes in model and have also set the validation message for them in the view but that required field validations are not working on popups. Required field validation is working fine on forms other than JQuery Popups....Please guide me that what should i do to tackle this issue...Following is my code.

型号

[Display(Name = "Material Code")]
[Required(ErrorMessage = "*")]
public string MaterialCode { get; set; }

查看

<li>
    @Html.LabelFor(m => m.MaterialCode)
    @Html.TextBoxFor(m => m.MaterialCode)
    @Html.HiddenFor(m => m.MaterialCodeId)
</li>

以下是我打开 JQuery 弹出窗口的代码.

and following is my cod eto open a JQuery popup.

$('#btnAddCharge').on('click', function (event) {  
        event.preventDefault();
        var actionURL = '@Url.Action("Edit", "Charges", new { Id = 0, @ticketId = @TicketId, UserId = UserId })';

        $(dialogBox).dialog({
            autoOpen: false,
            resizable: false,
            title: 'Edit',
            modal: true,
            show: "blind",
            width: 'auto',
            hide: "blind",
            open: function (event, ui) {
                $(this).load(actionURL, function (html) {
                    $('form', html).submit(function () {
                        $.ajax({
                            url: this.action,
                            type: this.method,
                            data: $(this).serialize(),
                            success: function (res) {
                                if (res.success) {
                                    $(dialogBox).dialog('close');
                                }
                            }
                        });
                        return false;
                    });
                });
            }
        });

        $(dialogBox).dialog('open');
    });

推荐答案

验证器在页面最初加载时被解析.添加动态内容时,您需要重新解析验证器.修改您的脚本以在加载内容后包含以下几行

The validator is parsed when the page is initially loaded. When you add dynamic content you need to reparse the validator. Modify your script to include the following lines after the content is loaded

$(this).load(actionURL, function (html) {
    // Reparse the validator
    var form = $('form');
    form.data('validator', null);
    $.validator.unobtrusive.parse(form);
    $('form', html).submit(function () {
        ....

旁注:您显示的代码不包括 @Html.ValidationMessageFor(m => m.MaterialCode) 但我认为这包括在内.

Side note: The code you have shown does not include @Html.ValidationMessageFor(m => m.MaterialCode) but I assume this is included.

这篇关于必填字段验证在 JQuery Popup MVC 4 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆