Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @using (Ajax.BeginForm("Login", new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "Login",InsertionMode = InsertionMode.Replace, OnSuccess = "Success", OnFailure = "onError" }))
- {
- }
- [httpPost]
- public ViewResult Login(LoginModel model)
- {
- if (ModelState.IsValid)
- {
- }
- else
- {
- ModelState.AddModelError("login is fail")
- }
- return View("Login",model)
- }
- [HttpPost]
- public ActionResult Login(LoginModel model)
- {
- if (ModelState.IsValid)
- {
- // everything went fine and we want to redirect in this case =>
- // we pass the url we want to redirect to as a JSON object:
- return Json(new { redirectTo = Url.Action("SomeController", "SomeAction") });
- }
- else
- {
- // there was an error => add an error message
- ModelState.AddModelError("login is fail")
- }
- // return a partial view instead of a full vire
- return PartialView("Login",model)
- }
- @using (Ajax.BeginForm("Login", new AjaxOptions { HttpMethod = "POST", OnSuccess = "loginAjaxSuccess" }))
- {
- }
- function loginAjaxSuccess(result) {
- if (result.redirectTo) {
- // the controller action returned a JSON result => it was a successful login
- // => we redirect the browser to this url
- window.location.href = result.redirectTo;
- } else {
- // the action returned a partial view with the form containing the errors
- // => we need to update the DOM:
- $('#Login').html(result);
- }
- }
- } else {
- // the action returned a partial view with the form containing the errors
- // => we need to update the DOM
- $('#Login').html(result);
- // Now that the DOM is updated let's refresh the unobtrusive validation rules on the form:
- $('form').removeData('validator');
- $('form').removeData('unobtrusiveValidation');
- $.validator.unobtrusive.parse('form');
- }
Add Comment
Please, Sign In to add comment