Guest User

Untitled

a guest
Jan 20th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. @using (Ajax.BeginForm("Login", new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "Login",InsertionMode = InsertionMode.Replace, OnSuccess = "Success", OnFailure = "onError" }))
  2. {
  3.  
  4. }
  5.  
  6. [httpPost]
  7. public ViewResult Login(LoginModel model)
  8. {
  9. if (ModelState.IsValid)
  10. {
  11.  
  12. }
  13. else
  14. {
  15. ModelState.AddModelError("login is fail")
  16. }
  17. return View("Login",model)
  18. }
  19.  
  20. [HttpPost]
  21. public ActionResult Login(LoginModel model)
  22. {
  23. if (ModelState.IsValid)
  24. {
  25. // everything went fine and we want to redirect in this case =>
  26. // we pass the url we want to redirect to as a JSON object:
  27. return Json(new { redirectTo = Url.Action("SomeController", "SomeAction") });
  28. }
  29. else
  30. {
  31. // there was an error => add an error message
  32. ModelState.AddModelError("login is fail")
  33. }
  34.  
  35. // return a partial view instead of a full vire
  36. return PartialView("Login",model)
  37. }
  38.  
  39. @using (Ajax.BeginForm("Login", new AjaxOptions { HttpMethod = "POST", OnSuccess = "loginAjaxSuccess" }))
  40. {
  41.  
  42. }
  43.  
  44. function loginAjaxSuccess(result) {
  45. if (result.redirectTo) {
  46. // the controller action returned a JSON result => it was a successful login
  47. // => we redirect the browser to this url
  48. window.location.href = result.redirectTo;
  49. } else {
  50. // the action returned a partial view with the form containing the errors
  51. // => we need to update the DOM:
  52. $('#Login').html(result);
  53. }
  54. }
  55.  
  56. } else {
  57. // the action returned a partial view with the form containing the errors
  58. // => we need to update the DOM
  59. $('#Login').html(result);
  60.  
  61. // Now that the DOM is updated let's refresh the unobtrusive validation rules on the form:
  62. $('form').removeData('validator');
  63. $('form').removeData('unobtrusiveValidation');
  64. $.validator.unobtrusive.parse('form');
  65. }
Add Comment
Please, Sign In to add comment