我有以下代码发布到我的控制器中的 httppost 方法:
$QuickLoginSubmit.click(function (e) {
e.preventDefault();
var LoginModel = {
'UserName': $QuickEmail.val() == "" ? null : $QuickEmail.val(),
'Password': $QuickPassword.val() == "" ? null : $QuickPassword.val(),
'RememberMe': $QuickRemember.val() == "on" ? true : false
};
$.ajax({
url: '/Account/LogOnAjax',
type: 'POST',
contentType: 'application/json',
dataType: 'json',
data: JSON.stringify(LoginModel),
success: function (result) {
if (result == true) {
window.location = "/Dashboard";
} else {
$QuickLoginErrors.text(result);
}
}
});
});
问题是,如果他们没有在$QuickEmail
or$QuickPassword
字段中输入任何内容,则 ajax 调用会返回以下错误:
The view 'LogOnAjax' or its master was not found or no view engine supports the searched locations. The following locations were searched:<br>~/Views/Account/LogOnAjax.aspx<br>~/Views/Account/LogOnAjax.ascx<br>~/Views/Shared/LogOnAjax.aspx<br>~/Views/Shared/LogOnAjax.ascx<br>~/Views/Account/LogOnAjax.cshtml<br>~/Views/Account/LogOnAjax.vbhtml<br>~/Views/Shared/LogOnAjax.cshtml<br>~/Views/Shared/LogOnAjax.vbhtml</i>
但是,如果填写了这两个字段,我的 ajax 方法调用就可以正常工作。这是我的httppost方法:
[HttpPost]
public ActionResult LogOnAjax(LogOnModel model)
{
if (ModelState.IsValid)
{
if (Membership.ValidateUser(model.UserName, model.Password))
{
FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
return Json(true);
}
else
{
return Json("The user name or password provided is incorrect.");
}
}
// If we got this far, something failed, redisplay form
return View(model);
}