如何在MVC3中的JQuery ajax调用上重定向到新页面(以及模型)

我忘记了密码页面,用户输入用户名并单击“validation”按钮以检查他所在的组。根据组,我们需要显示不同的部分视图(现在假设它是电话号码)这页纸。 在填写有效详细信息后, 成功后我将重定向到他将更新密码的新页面,在失败时,我需要显示错误消息。

现在我很难写出突出显示的代码。

这是Jquery ajax函数的代码,它在提交按钮单击时触发

person = {UserName: $("#UserName").val(), Phone: $("#Phone").val() } $.ajax({ type: 'POST', url: '@Url.Action("ForgotPassword", "Customer")', data: person, dataType: 'json', success: function (data) { //This is the place I need help if(data.IsDataValid){ // redirect to new page passing the model object } else{ //Show an error message without hiding the div } }, failure: function () { } }); 

这是代码控制器动作

  [HttpPost] [ValidateInput(true)] public ActionResult ForgotPassword(RegisterModel model) { if (Request.IsAjaxRequest()) { Here is the logic which validates the user return Json(new { regmodel = RegistrationModel }) } //If it is not ajax call I want to return the view if (isUsergroup1 == true) { return View("View1", RegistrationModel ); } else if (isUsergroup2 == true) { return View("View2", RegistrationModel ); } else { return View(); } } 

请帮忙。 提前致谢

这就是我们需要做的事情。 我们无法传递模型,我们只能在链接的重定向中传递参数。 我们必须使用link.replace,因为您可以将变量直接传递给Url.Action

 person = {UserName: $("#UserName").val(), Phone: $("#Phone").val() } $.ajax({ type: 'POST', url: '@Url.Action("ForgotPassword", "Customer")', data: person, dataType: 'json', success: function (data) { //This is the place I need help if(IsDataValid){ var link = "@Url.Action("Actionname", "Controllername", new { username = "1", phone ="2" })"; link = link.replace("1", data.username); link = link.replace("2", data.phone); alert(link); window.location.href= link ; } else{ //Show an error message without hiding the div } }, failure: function () { } }); 

解决您现在负责的部分问题:

 if(IsDataValid){ // redirect to new page( } else{ //Show an error message without hiding the div } 

要重定向,请使用window.location.replace('url to new page');

对于“错误消息”部分,我要么使用jQuery UI来显示对话框,要么使用隐藏的div来插入错误消息,或许是这样:

 $('#errors').html(data.Errors).show(); 

这假设您有一些名为Errors的属性,其中包含RegistrationModel上的validation消息。