2012-12-01 50 views
1
@using (Ajax.BeginForm("Login", "Account", "", 
      new AjaxOptions { HttpMethod = "POST" }, 
      new { id = "loginForm", name = "loginForm" })) 
{ 
... 
} 

此表单执行请求并接收响应200 OK。 Debbuging我可以看到响应html,但我没有被重定向。@ Ajax.BeginForm请求/响应正常但控制器没有重定向

如果我这样做,而不使用HTML帮助我成功地重定向到我需要的地方。

这是控制器:

// 
    // POST: /Account/Login 
    [HttpPost]   
    [AllowAnonymous] 
    [ValidateAntiForgeryToken] 
    public ActionResult Login(LoginModel model) 
    { 
     MembershipProvider mp = Membership.Provider; 
     bool isDigital = bool.Parse(Request.Form.GetValues("hasDigital")[0]); 
     string certDBNumber; 

     if (isDigital) 
     { 
      /*** Retira só o que enteressa do Certificado.Subject (CPF/CNPJ)*/ 
      string code = Request.Form.GetValues("code")[0]; 
      string[] dataArray = code.Split(','); 
      string data = dataArray.Last(); 
      string[] numberArr = data.Split(':'); 
      string number = numberArr.Last(); 

      /*** Resgata chave do usuário no banco ***/ 
      using (tgpwebgedEntities context = new tgpwebgedEntities()) 
      { 
       var userObj = from u in context.aspnet_Users 
           where u.UserName == model.UserName 
           select u.UserId; 
       Guid userID = userObj.First(); 
       var obj = from u in context.sistema_UsersCertified 
          where u.userID == userID select u.keyNumber; 
       certDBNumber = obj.First(); 
      } 

      //Verifica se usuário é credenciado 
      if (number == certDBNumber) { 
       FormsAuthentication.SetAuthCookie(model.UserName, false); 
       return RedirectToAction("Index", "Home");      
      } 
     }//Login sem certificado digital 
     else 
     { 
      if (ModelState.IsValid && 
          mp.ValidateUser(model.UserName, model.Password)) 
      { 
       FormsAuthentication.SetAuthCookie(model.UserName, false); 
       return RedirectToAction("Index", "Home"); 
      } 
     } 
     /*** Se chegou até aqui algo deu errado. Mostra denovo o form ***/ 
     ModelState.AddModelError("", "Username ou Password incorreto!."); 
     return View(model); 
    } 

为什么这种奇怪的行为?

回答

6

由于这是一个ajax文章,你不能重定向。一种选择是返回一个值,从而成功实现的功能要发生所需的重定向,然后发出

if(someReturnedFlag == true){ 
window.location = redirectionUrl;//http://www.stackoverflow.com 
} 

或者,你可以做一个重定向视图

RedirectionView.cshtml

@{ 
Layout = null; 
} 
<script type="text/javascript"> 
window.location = "hardcodedurl";//or pass it in using @model 
</script> 

然后从你的ajax文章返回这个视图,它会重定向。


编辑

这引起了更多的关注比我预想的,所以我想我会改善这个回答有点有两个较为完整的例子。


1:jQuery的AJAX

视图:

$.ajax({ 
     url: "@(Url.Action("TargetAction","TargetController"))", 
     type: 'POST', 
     data: $("#loginForm").serialize(), 
     success: function (URL) { 
     window.location = URL; 
     } 
}); 

控制器:

public class TargetController: Controller 
[HttpPost] 
public string TargetAction(ViewModel model) 
{ 
//use model (note that the serialized form was bound to the model) 
return "http://www.stackoverflow.com"; 
} 

2:重定向ionView.cshtml

的主视图:

@{ 
AjaxOptions ajaxOpts = new AjaxOptions 
{ 
    UpdateTargetId = "redirectWhenDone" 
}; 
} 

@using (Ajax.BeginForm("TargetAction", ajaxOpts)) 
{ 
... 
} 
<div id="redirectWhenDone"></div> 

RedirectionView.cshtml

@model string 
@{ 
Layout = null; 
} 
<script type="text/javascript"> 
window.location = "@(Model)"; 
</script> 

控制器

[HttpPost] 
public ActionResult TargetAction(ViewModel vm) 
{ 
//use ViewModel 

... 


//it is important to use an object here for the string 
object url = "http://www.stackoverflow.com"; 
//otherwise the View() method will consider the string a parent location and look 
//for the RedirectionView in the wrong place (basically using the wrong overload) 
return View("RedirectionView", url); 
}