Why always fires OnFailure when return View() to Ajax Form ?
- by Wahid Bitar
I'm trying to make a log-in log-off with Ajax supported.
I made some logic in my controller to sign the user in and then return simple partial containing welcome message and log-Off ActionLink my Action method looks like this :
public ActionResult LogOn(LogOnModel model, string returnUrl)
{
if (ModelState.IsValid)
{
if (MembershipService.ValidateUser(model.UserName, model.Password))
{
FormsService.SignIn(model.UserName, model.RememberMe);
if (Request.IsAjaxRequest())
{
//HERE IS THE PROBLEM :(
return View("LogedInForm");
}
else
{
if (!String.IsNullOrEmpty(returnUrl))
return Redirect(returnUrl);
else
return RedirectToAction("Index", "Home");
}
}
else
{
ModelState.AddModelError("", "The user name or password provided is incorrect.");
if (Request.IsAjaxRequest())
{
return Content("There were an error !");
}
}
}
return View(model);
}
and I'm trying to return this simple partial :
Welcome <b><%= Html.Encode(Model.UserName)%></b>!
<%= Html.ActionLink("Log Off", "LogOff", "Account") %>
and of-course the two partial are strongly-typed to LogOnModel.
But if i returned View("PartialName") i always get OnFailure with status code 500.
While if i returned Content("My Message") everything is going right.
so please tell me why i always get this "StatusCode = 500" ??. where is the big mistake ??.
By the way in my Site MasterPage i rendered partial to show long-on simple form this partial looks like this :
<script type="text/javascript">
function ShowErrorMessage(ajaxContext) {
var response = ajaxContext.get_response();
var statusCode = response.get_statusCode();
alert("Sorry, the request failed with status code " + statusCode);
}
function ShowSuccessMessage() {
alert("Hey everything is OK!");
}
</script>
<div id="logedInDiv">
</div>
<% using (Ajax.BeginForm("LogOn", "Account", new AjaxOptions
{
UpdateTargetId = "logedInDiv",
InsertionMode = InsertionMode.Replace,
OnSuccess = "ShowSuccessMessage",
OnFailure = "ShowErrorMessage"
}))
{ %>
<%= Html.TextBoxFor(m => m.UserName)%>
<%= Html.PasswordFor(m => m.Password)%>
<%= Html.CheckBoxFor(m => m.RememberMe)%>
<input type="submit" value="Log On" />
< <% } %>