【问题标题】:JQuery modal box form validationJQuery模态框表单验证
【发布时间】:2010-04-14 15:09:30
【问题描述】:

我以前使用视图为我的特定项目创建一个对象,但现在我必须使其适应另一个要求,例如这个对象的创建必须集成到另一个对象的创建中。问题是,这个对象必须在开始创建另一个对象之前创建,这样做的方法是弹出一个带有 JQuery 的模式对话框,其中包含创建它的表单。我必须调整创作,它工作正常,除了验证消息。它是在控制器中制作的:

if (not.NotName.Trim().Length == 0)
{
    ModelState.AddModelError("NotName", "Name is required");
}
if (_notification.checkIfExists(not.NotName, not.NotRecID))
{
    ModelState.AddModelError("NotName", "Notification already exists");
}

在正常视图之前它工作得很好,但现在我无法像我一样获得验证消息。你怎么能得到这个控制器在模态框中抛出的验证消息?因为现在当我为了得到错误而故意在表单中犯了错误时,它不会出现。我已经看到错误显示在站点中,因为我在 firebug 上看到过它,但它没有出现在我的模型框中。如何检索此错误?你知道任何可以帮助我的教程吗?

javascript方法是:

function newNotificationModalBox() {
    $("#newNotificationModalBox").dialog("destroy");

    $("#newNotificationModalBox").dialog({
        modal: true,
        open: function(event, ui) {
            //resetNotificationForm();
        },
        buttons: {
            'Create': function() {
                var name = document.getElementById("NotName").value;
                var status = document.getElementById("NotificationStatus").value;
                var replace = document.getElementById("ReplaceYes").checked;
                var notificationToReplace = document.getElementById("ReplaceNotificationID").value;
                var dataString = 'NotName=' + name + '&NotificationStatus=' + status + '&Replace=' + replace + '&ReplaceNotificationID=' + notificationToReplace;
                $.ajax({
                    type: "POST",
                    url: "/Suspension/CreateNotification",
                    data: dataString
                    });
                var list = document.getElementById("sNotification").options.length;
                loadNotifications("/SearchSuspensions/LoadNotifications/", "A", "sNotification", "pNotificationName");
                alert('submitting form');
                //alert(document.getElementById("sNotification").options.length);
                if (list != document.getElementById("sNotification").options.length) {
                    $(this).dialog('close');
                    setTimeout('selectNotificationCreated();', 100);
                    alert('Notification succesfully created');
                }
                else {
                    alert('Error');
                }
            },
            'Cancel': function() {
                $(this).dialog('close');
            }
        }
    });

}

谢谢

【问题讨论】:

    标签: asp.net jquery asp.net-mvc


    【解决方案1】:

    一种可能性是创建一个呈现表单的局部视图。然后创建一个将呈现局部视图的操作。现在使用 AJAX 将部分视图加载到模式中。当您发布时,模态将通过 AJAX 发布表单。当帖子返回时,您要么成功,要么应该将带有错误的表单返回给您,从而允许您在模式中重新加载表单和错误消息。

    【讨论】:

    • 我理解你给我的步骤,其实这就是我正在做的。我的问题是当我到达最后一部分时,我的意思是,当我必须显示在 ajax 调用之后从控制器发回的表单时。有没有办法刷新模态以获得这个结果?为了更清楚一点,我已经发布了 javascript 方法。谢谢
    • 看起来你在使用 Jquery。尝试使用 jquery.forms 和 ajaxSubmit 功能。这有一个完整的函数,允许你编写一个在回发发生时发生的函数。
    猜你喜欢
    • 2021-10-24
    • 1970-01-01
    • 2015-01-27
    • 1970-01-01
    • 2014-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多