Guest User

Untitled

a guest
Aug 1st, 2018
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1. asp.net mvc partial view redirect or show error
  2. @{
  3. var ajaxOpts = new AjaxOptions {OnSuccess = "success"};
  4. }
  5.  
  6. <div id="login">
  7. @Html.ValidationSummary(excludePropertyErrors: true)
  8.  
  9. <h2>Start by Logging in</h2>
  10.  
  11. @using (Ajax.BeginForm("LogOn", "Account", ajaxOpts))
  12. {
  13. @Html.Hidden("returnUrl", Request.Url.PathAndQuery)
  14.  
  15. <table width="100%" border="0" cellspacing="0" cellpadding="5">
  16. <tr>
  17. <td>
  18. <span class="bluey">Username:</span><br />
  19. @Html.TextBoxFor(m => m.UserName, new {tabindex = "1", Class = "field"})
  20. @Html.ValidationMessageFor(m => m.UserName, "*")
  21. </td>
  22. <td>
  23. <span class="bluey">Password:</span><br />
  24. @Html.TextBoxFor(m => m.Password, new {tabindex = "2", Class = "field"})
  25. @Html.ValidationMessageFor(m => m.Password, "*")
  26. </td>
  27. </tr>
  28. <tr>
  29. <td>
  30. <input name="login" type="submit" value="Submit" class="input_btn" tabindex="3" />
  31. </td>
  32. <td>@Html.CheckBoxFor(m => m.RememberMe) @Html.LabelFor(m => m.RememberMe) <span class="bluey">&nbsp; | &nbsp;</span> @Html.ActionLink("Forgot Password?", "Password", "User")</td>
  33. </tr>
  34. </table>
  35. }
  36. </div>
  37.  
  38. <script>
  39. function success(context) {
  40. //var returnUrl = context.get_data().returnUrl;
  41. //if (returnUrl) {
  42. // window.location.href = returnUrl;
  43. //} else {
  44. $("#login").replaceWith(context);
  45. //}
  46.  
  47. }
  48.  
  49. </script>
  50.  
  51. [HttpPost]
  52. public ActionResult LogOn(LogOnModel userDetails, string returnUrl)
  53. {
  54. if (ModelState.IsValid)
  55. {
  56. if (Membership.ValidateUser(userDetails.UserName, userDetails.Password))
  57. {
  58. FormsAuthentication.SetAuthCookie(userDetails.UserName, userDetails.RememberMe);
  59.  
  60. return Redirect(returnUrl);
  61. }
  62. ModelState.AddModelError("", "The username or password provided was incorrect");
  63. }
  64.  
  65. return PartialView(userDetails);
  66. }
  67.  
  68. function success(result) {
  69. if (result.returnUrl) {
  70. // the controller action returned JSON
  71. window.location.href = returnUrl;
  72. } else {
  73. // the controller action returned a partial
  74. $('#login').html(result);
  75. }
  76. }
  77.  
  78. return Json(new { returnUrl = returnUrl });
Add Comment
Please, Sign In to add comment