我有一個模式對話框(使用jquery simplemodal)顯示登錄視圖,並且當用戶登錄失敗 - 工作正常 - 錯誤返回到模式。問題是當用戶成功登錄時 - 頁面應該被重定向並且模式消失,但是模式現在用整個頁面更新:(ASP.NET MVC登錄模態問題 - 重定向轉到模態?
爲了使模式看起來正確 - 我檢查在AccountController中查看登錄的源是否是Ajax,如果是 - 返回一個局部視圖(以模態顯示),如果不是,則返回完整視圖(以防用戶嘗試從網站外部訪問頁面)
public ActionResult LogOn()
{
if (Request.IsAjaxRequest())
{
return PartialView("_LogOn");
}
return View();
}
[HttpPost]
public ActionResult LogOn(LogOnModel model, string returnUrl)
{
if (ModelState.IsValid)
{
if (MembershipService.ValidateUser(model.UserName, model.Password))
{
FormsService.SignIn(model.UserName, model.RememberMe);
if (!String.IsNullOrEmpty(returnUrl))
{
return Redirect(returnUrl);
}
else
{
return RedirectToAction("Index", "Home");
}
}
else
{
ModelState.AddModelError("", "The user name or password provided is incorrect.");
}
}
// If we got this far, something failed, redisplay form
if (Request.IsAjaxRequest())
{
return PartialView("_LogOn");
}
return View();
}
在我的登錄用戶控件(在的Site.Master那個那)我添加了一個腳本JavaScript來登錄登錄鏈接被點擊
時雖然局部視圖(即大幹快上的Ajax調用返回)看起來是這樣的:
<% using (Ajax.BeginForm("LogOn", "Account", new AjaxOptions{UpdateTargetId = "logonForm"})) { %>
<div id="logonForm">
<%: Html.ValidationSummary(true, "Login was unsuccessful. Please review and try again.") %>
<p>Please enter your username and password.
</p>
<p>
<input id="submit" type="submit" value="Log In" />
</p>
</div>
<%}%>
所以問題是,AjaxOptions UpdateTargetId是logonForm - 這是我想要什麼時,用戶登錄失敗,但當成功登錄..我希望它正常更新頁面 - 但我不能區分ajax登錄和AccountController.LogOn正常登錄,除非我這樣做:P
有任何想法嗎?希望這是有道理的 - 謝謝
它像im im擰任何一種方式。