我有一个模式对话框(使用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拧任何一种方式。