Javascript 在MVC验证之后执行Ajax

Javascript 在MVC验证之后执行Ajax,javascript,ajax,asp.net-mvc,twitter-bootstrap,modal-dialog,Javascript,Ajax,Asp.net Mvc,Twitter Bootstrap,Modal Dialog,我有一个表单,在这个表单中,我有来自模型的字段。 该表单是来自引导的模式对话框。当我点击提交按钮时,所有字段都必须有效。这管用! 现在,如果模型验证有效,我想关闭模式并刷新页面。只有代码首先执行Ajax调用,然后在控制器中执行ModelState.IsValid 阿贾克斯: 有人知道我能做些什么吗?或者这是不可能的 编辑: 或者如果,是否可以在中调用Ajax函数: @if (ViewData.ModelState.IsValid) { //Call her

我有一个表单,在这个表单中,我有来自模型的字段。 该表单是来自引导的模式对话框。当我点击提交按钮时,所有字段都必须有效。这管用! 现在,如果模型验证有效,我想关闭模式并刷新页面。只有代码首先执行Ajax调用,然后在控制器中执行
ModelState.IsValid

阿贾克斯:

有人知道我能做些什么吗?或者这是不可能的

编辑:

或者如果,是否可以在
中调用Ajax函数:

        @if (ViewData.ModelState.IsValid) {
            //Call here the functions
            refreshAlerts(); //Error: The name 'refreshAlerts' does not exist in the current context
        }

尝试在视图中使用
ViewData.ModelState.IsValid

$('#addAlertModal form').submit(function (e) {

    @if (ViewData.ModelState.IsValid)
    {
         refreshAlerts();
        closeAlertModal();

    }
});

尝试在视图中使用
ViewData.ModelState.IsValid

$('#addAlertModal form').submit(function (e) {

    @if (ViewData.ModelState.IsValid)
    {
         refreshAlerts();
        closeAlertModal();

    }
});
试着这样,

 $("#YourbuttonId").click(function () {

                       var validation = $("#FormId"); 
                        if (!validation.valid()) {
                              return false;
                         }
                         else {
                            refreshAlerts();
                           closeAlertModal();
                          }
                     });
试着这样,

 $("#YourbuttonId").click(function () {

                       var validation = $("#FormId"); 
                        if (!validation.valid()) {
                              return false;
                         }
                         else {
                            refreshAlerts();
                           closeAlertModal();
                          }
                     });

为什么不使用事件,以便在验证正常后关闭页面并刷新。你需要使用触发器,仅此而已


为什么不使用事件,以便在验证正常后关闭页面并刷新。你需要使用触发器,仅此而已

我有:

@using (Ajax.BeginForm("Index", "ManageAlert", FormMethod.Post, new AjaxOptions { UpdateTargetId = "addAlertModal", InsertionMode = InsertionMode.Replace, OnSuccess = "afterSubmit(data)"}))
{ ... }

function afterSubmit(data) {
    if (data.success === true) {
        refreshAlerts();
        closeAlertModal();
    }
}
在模型中,我返回:

return Json(new { success = true });
我有:

@using (Ajax.BeginForm("Index", "ManageAlert", FormMethod.Post, new AjaxOptions { UpdateTargetId = "addAlertModal", InsertionMode = InsertionMode.Replace, OnSuccess = "afterSubmit(data)"}))
{ ... }

function afterSubmit(data) {
    if (data.success === true) {
        refreshAlerts();
        closeAlertModal();
    }
}
在模型中,我返回:

return Json(new { success = true });

那不行!我的要求在模型中,而不是在视图中!现在验证将始终为真。这不起作用!我的要求在模型中,而不是在视图中!现在验证总是正确的。好吧,我现在知道
@if(ViewData.ModelState.IsValid)
有效,但是现在我不能调用ajax方法。我现在知道
@if(ViewData.ModelState.IsValid)
有效,但是现在我不能调用ajax方法我不知道你的意思!你能用密码让我看看吗?我不知道你到底是什么意思!你能给我一些代码看看吗?