上下文:Asp.Net MVC3 w / Razor
我试图把一个登录表单在Razor布局(以前的主页),这样,当用户超时,他/他可以提示登录而不被重定向(如在RallyDev)。所以,我创建了一个部分_LogOn.cshtml与所有必要的东西(用户名等)在一个Ajax.BeginForm与UpdateTargetId,指向一个div,其中包含在ajax形式的登录控件。该表单发回到AccountsController.RefreshLogOn操作,但显然,包含页面可能已从不同的控制器呈现。 RefreshLogOn操作返回PartialView(“_ LogOn”)。在任何情况下,我的期望/愿望是只有这个div内的控件被替换。发生了什么,而是我的页面位置更改为/ Accounts / RefreshLogon,整个页面替换为partial。有没有另一种方法,我应该采取?
以下是相关代码:
_LogOn.cshtml
@{
using (Ajax.BeginForm("RefreshLogOn", "Accounts",
new AjaxOptions {
OnSuccess = "logonSucceeded",
OnFailure = "logonFailed",
HttpMethod = "Post",
UpdateTargetId = "user-info" },
new { @id = "refresh"}))
{
@Html.AntiForgeryToken()
Your session has expired.
Username: | @Html.TextBoxFor(model => model.UserName) |
Password: | @Html.PasswordFor(model => model.Password) |
Remember Me: | @Html.CheckBoxFor(model => model.RememberMe) |
}
}
AccountsController
public ActionResult RefreshLogOn (LogOnModel model)
{
if (ModelState.IsValid)
{
if (MembershipService.ValidateUser(model.UserName, model.Password))
{
...content elided...
return PartialView("_LogOn");
}
ModelState.AddModelError("", ErrorMessages.IncorrectUsernameOrPassword);
}
return PartialView("_LogOn", model);
}
Ajax.BeginForm – >表单标签生成:
οnclick="Sys.Mvc.AsyncForm.handleClick(this, new Sys.UI.DomEvent(event));"
οnsubmit="Sys.Mvc.AsyncForm.handleSubmit(this, new Sys.UI.DomEvent(event),
{
insertionMode: Sys.Mvc.InsertionMode.replace,
httpMethod: 'Post',
updateTargetId: 'user-info',
onFailure: Function.createDelegate(this, logonFailed),
onSuccess: Function.createDelegate(this, logonSucceeded)
});">