2013-06-26 45 views
2

嗨我的页面有问题。我在同一页面中有一个视图页面和两个表单。使用jQuery弹出对话框问题

问题是我有一个主窗体和另一个窗体,它是由JQuery显示的。我的问题是当我打开对话框,提交它的窗体并返回视图时,对话框会出现。我不知道如何返回结果,它仍然会显示打开的对话框。

我需要你的帮助!

下面是我在我的应用程序中使用的代码。

.CSTHML窗体

@using (Html.BeginForm("Login2", "Account", FormMethod.Post, new { @id = "loginForm" })) 
{ 
    <a id="submitlink" href="#" class="button button-large">Sign In</a> 
} 

// This is the pop-up dialog box 
@using (Html.BeginForm("TroubleSubmit", "Account", FormMethod.Post, new { @id = "troubleSubmitForm" })) 
{ 
    <a id="troubleSubmitLink" href="#" class="button">OK</a> 
} 

JQUERY

$('a#submitlink').click(function() { 
      $('#loginForm').submit(); 
     }); 

$('a#troubleSubmitlink').click(function() { 
      $('#troubleSubmitForm').submit(); 
     }); 

下面是我的控制器动作来处理的代码的对话框形式提交:

public ActionResult SignInTrouble(some parameter...) 
    { 
     // some operation and validation here 

     return View(); // ??? What view will I return? When I return "Login" it will reload the page and close the dialog box. 
    } 

同样,怎么办我返回的视图仍然会显示对话框

+1

一旦你提交页面重新加载的形式。如果您不想重新加载页面,则应该使用AJAX请求。 –

+0

或iFrame,我认为jQuery对话框处理它们 –

+0

你能给我一些示例代码吗? – Mat

回答

0

你提交表单在传统(非AJAX)的方式使该页面将重新加载。所以你需要以AJAX的方式发布。

$("#submitlink").on("click", function() { 
    var loginForm = $("#loginForm"); 

    $.ajax({ 
     url: loginForm.attr("action"), 
     type: "post", 
     data: loginForm.serialize(), 
    }) 
    .done(function(result) { 
     // do something with the returned response 
    }); 
}); 

成功的响应与.done()处理,但你回来什么,你是如何处理的结果是你。最简单的选择是返回一个局部视图,因此它只是一个插入到现有div容器中的html片段。

.done(function(result) { 
    $("#someDiv").html(result); 
}); 

我经常返回JSON,其视图呈现为html字符串{ status: 0, message: "Success", html: "<div>..." }。如果你只需要一个简单的YES/NO,你可以省略JSON中的html片段。

public ActionResult SignInTrouble(some parameter...) 
{ 
    // some operation and validation here 

    return Json({ 
     status: 1, 
     message: "Validation Error" 
    }); 
} 

然后你得到一些更多的可能性与您回应

.done(function(result) { 
    var status = result.status; 
    var message = result.message; 
    if (status == 0) { 
     // OK 
    } else { 
     // ERROR 
    } 
    $("#messageDiv").text(message); 
} 
0

它只是由于页面被重新加载,在这种情况下您必须使用ajax表单,所以它只会处理ajax表单的动作,然后将结果返回给没有形式重新加载页面

@Ajax.BeginForm("TroubleSubmit","Account", new AjaxOptions(){ ...... }) 
+0

你的意思是说,用你的建议替换这个“@using(Html.BeginForm(”TroubleSubmit“,”Account“,FormMethod.Post,new {@id =”troubleSubmitForm“}))”,对吧?我的意思是 – Mat

+0

,但是你必须在AjaxOptions选项中改变一些东西来适应你的需求。 –

+0

我现在能够使Ajax.BeginForm工作,我现在的问题是如何返回一个验证错误消息到对话框? – Mat