I am creating a new asp.net mvc3 project with Razor and want to turn LogOn into ajax request.
HTML
@using (Ajax.BeginForm("LogOn", "Account", new AjaxOptions { HttpMethod="post", OnSuccess="LoginSubmitted"})) { }
controller
if (Request.IsAjaxRequest()) { return Json(new { ResultMessage = "Username or password provided is incorrect"}); } else { ModelState.AddModelError("", "The user name or password provided is incorrect."); }
Everything else remains the same.
First, looking at the http response from Fiddler, I notice that the header with the request x is missing. Therefore i add
<input type="hidden" name="X-Requested-With" value="XMLHttpRequest" />
This seems to work, but now what I get is a Json object that is not parsed, and instead Google Chrome just displays Json on the screen, sending back the / json doc application. All scripts are in place.
I also did this:
@using (Ajax.BeginForm("Submit", "home", new AjaxOptions { HttpMethod = "Post", OnSuccess="LoginSubmitted"})) { } @section head { <script type="text/javascript"> function LoginSubmitted(res) { alert(res.Message); } </script> } public ActionResult Submit(string id) { if (Request.IsAjaxRequest()) { return Json(new { Message = "Logged In" } ); } else { return View(); } }
In the form of my own creation, which works great with standard helpers.
What's happening?
source share