上下文:Asp.Net MVC3 w/Razor
我试图在Razor布局(以前的母版页)上放置一个登录表单,这样,当用户超时时,可以提示他/她登录而不被重定向(如在RallyDev中).因此,我在Ajax.BeginForm中创建了一个部分_LogOn.cshtml,其中包含所有必需的东西(用户名等),其中UpdateTargetId指向一个div,该div包含ajax表单内的登录控件.表单回发到AccountsController.RefreshLogOn操作,但显然,包含的页面可能是从不同的控制器呈现的.RefreshLogOn操作返回PartialView("_ LogOn").在任何情况下,我的期望/愿望是只替换此div中的控件.相反,我的页面位置更改为/ Accounts/RefreshLogon,整个页面被部分替换.我应该采取另一种方法吗?
这是相关的代码:
_LogOn.cshtml
@{
using (Ajax.BeginForm("RefreshLogOn", "Accounts",
new AjaxOptions {
OnSuccess = "logonSucceeded",
OnFailure = "logonFailed",
HttpMethod = "Post",
UpdateTargetId = "user-info" },
new { @id = "refresh"}))
{
@Html.AntiForgeryToken()
<div id="user-info">
<p>Your session has expired.</p>
<div class="error">@Html.ValidationSummary()</div>
<table>
<tr>
<td>Username:</td>
<td>@Html.TextBoxFor(model => model.UserName)</td>
</tr>
<tr>
<td>Password:</td>
<td>@Html.PasswordFor(model => model.Password)</td>
</tr>
<tr>
<td>Remember Me:</td>
<td>@Html.CheckBoxFor(model => model.RememberMe)</td>
</tr>
</table>
</div>
}
}
Run Code Online (Sandbox Code Playgroud)
AccountsController
public ActionResult RefreshLogOn (LogOnModel model)
{
if (ModelState.IsValid)
{
if (MembershipService.ValidateUser(model.UserName, model.Password)) …Run Code Online (Sandbox Code Playgroud) 我在我的控制器中声明了一个POST方法:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult UpdateComments(int id, string comments)
{
// ...
}
Run Code Online (Sandbox Code Playgroud)
和我认为的ActionLink:
<%= Ajax.ActionLink("update", "UpdateComments",
new { id = Model.Id, comments = "test" },
new AjaxOptions {
HttpMethod="POST",
OnFailure="alert('fail');",
OnSuccess = "alert('success');"
})%>
Run Code Online (Sandbox Code Playgroud)
尝试路由此请求时出现"未找到"错误.
如果我从控制器中的UpdateComments方法中删除POST限制,它可以正常工作.
我错过了什么?
我已经将ASP.NET MVC 3中的应用程序更新为ASP.NET MVC 4.
该应用程序在MVC 3中运行良好.唯一不适用于MVC 4的是Ajax.Begin形式:表单默认为完整页面请求,而不是异步AJAX请求.
从本质上讲,它是我写的一个向导,但这是无关紧要的.Model.Step.ActionName正确返回一个字符串(参见下面的代码).
视图中的代码是:
@{
using (Ajax.BeginForm(Model.Step.ActionName, null, new AjaxOptions { UpdateTargetId = "wizardStep", OnBegin="isValid", LoadingElementId="PleaseWait", OnSuccess="SetFocusOnForm" },
new {@name="wizardForm", @id="wizardForm" } ))
{
//form contents
}
}
Run Code Online (Sandbox Code Playgroud)
我注意到MVC 4中的Ajax.BeginForm使用HTML 5.我展示了MVC 3和MVC 4如何呈现下面的表单之间的区别:
MVC 3:
<form action="/Solicitors/Company/New/YourDetails" id="wizardForm" method="post" name="wizardForm" onclick="Sys.Mvc.AsyncForm.handleClick(this, new Sys.UI.DomEvent(event));" onsubmit="Sys.Mvc.AsyncForm.handleSubmit(this, new Sys.UI.DomEvent(event), { insertionMode: Sys.Mvc.InsertionMode.replace, loadingElementId: 'PleaseWait', updateTargetId: 'wizardStep', onBegin: Function.createDelegate(this, isValid), onSuccess: Function.createDelegate(this, SetFocusOnForm) });">
// form contents
</form>
Run Code Online (Sandbox Code Playgroud)
MVC 4:
<form action="/Solicitors/Company/New/LoginDetails" data-ajax="true" data-ajax-begin="isValid" data-ajax-loading="#PleaseWait" …Run Code Online (Sandbox Code Playgroud)