我有一個項目是使用引導莫代爾作爲登錄部分視圖,但我想有一個異步解決方案來驗證用戶登錄,所以通過jquery.post
傳遞數據行動和,但我發現那裏jquery.post
成功完成但沒有行動的迴應。jquery ajax dosen't在asp.net工作mvc
查看:
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="myModalLabel">Login </h4>
</div>
<div class="container">
<div class="modal-body" id="signinForm">
@using (Ajax.BeginForm("Login", "Account", new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "signinForm" }, new { @id = "targetForm" }))
{
// Html.BeginForm("Login", "Account", FormMethod.Post, new { @id = "signinForm" });
@Html.ValidationSummary(true, "Account creation was unsuccessful. Please correct the errors and try again.")
<div id="errorresult"></div>
<div class="control-group">
<label class="control-label" for="inputEmail">User Name</label>
<div class="controls">
@Html.TextBoxFor(m => m.UserName, new { @name = "inputUser", @class = "form-control", @id = "inputUser", @placeholder = "User Name" })
@Html.ValidationMessageFor(m => m.UserName)
</div>
</div>
<div class="control-group">
<label class="control-label" for="inputPassword">Password</label>
<div class="controls">
@Html.TextBoxFor(m => m.Password, new { @name = "inputPassword", @class = "form-control", @id = "inputPassword", @placeholder = "Password", @type = "password" })
@Html.ValidationMessageFor(m => m.Password)
</div>
</div>
<div class="control-group">
<div class="controls">
<label class="checkbox">
Remember me @Html.CheckBoxFor(m => m.RememberMe, new {@id="rememberMe"})
<span class="span9">@Html.ActionLink("Register", "Register", "Account") </span>
</label>
<button type="submit" id="submit_btn" class="btn btn-primary">sign in</button>
</div>
</div>
}
</div>
</div>
</div>
<!-- /.modal-content -->
</div>
<!-- /.modal-dialog -->
</div>
的Javascript:
$('#submit_btn').click(function (event) {
event.preventDefault();
var targetForm = $('#targetForm').serialize();
var newtargetForm= targetForm.replace(/&/g, "?");
$.post("@Url.Action("AjaxLogin", "Account")?"+ newtargetForm, {
userName: $('#targetForm #inputUser').val(),
passWord: $('#targetForm #inputPassword').val(),
rememberMe:$('#targetForm #rememberMe').is(':checked')
}, function (data, textStatus, xhr) {
/*optional stuff to do after success */
console.log(textStatus);
})
})
我的行動:
[HttpPost]
public ActionResult AjaxLogin(string UserName, string Password, bool RememberMe)
{
if (ModelState.IsValid)
{
if (Membership.ValidateUser(UserName,Password))
{
FormsAuthentication.SetAuthCookie(UserName, RememberMe);
return RedirectToAction("BlogPost","Blog");
}
}
return PartialView("Login");
}
從我的理解,asp.net mvc的途徑規則應該工作,但它沒」噸。
你的瀏覽器控制檯說什麼? – Shyju
console.log(textStatus);說「成功」 – robin521
我會啓動FireBug,看看會發生什麼。我不認爲你可以/應該像這樣混合使用jQuery和MVC Ajax。我沒有使用過MS Ajax的東西,但看起來他們都試圖同時處理同一個提交。 – Chris