Guest User

Untitled

a guest
Aug 14th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. In MVC, how to determine if partial view response was valid (on the client side)?
  2. [SomeController.cs]
  3. public class SomeController : Controller
  4. {
  5. public ActionResult ShowForm()
  6. {
  7. return View();
  8. }
  9. public ActionResult ValidateForm(MyFormModel FormModel)
  10. {
  11. FormValidationResults result = new FormValidationResults();
  12. result.IsValid = ModelState.IsValid;
  13. if (result.IsValid)
  14. {
  15. result.RedirectToUrl = "/Controller/Action";
  16. }
  17. this.Json(result);
  18. }
  19. }
  20.  
  21. [FormValidationResult.cs]
  22. public class FormValidationResults
  23. {
  24. public bool IsValid { get; set; }
  25. public string RedirectToUrl { get; set; }
  26. }
  27.  
  28. [View.js]
  29. $(document).ready(function()
  30. {
  31. $("#button").click(function()
  32. {
  33. var form = $("#myForm");
  34. $.ajax(
  35. {
  36. url: '/Some/ValidateForm',
  37. type: 'POST',
  38. data: form.serialize(),
  39. success: function(jsonResult)
  40. {
  41. if (jsonResult.IsValid)
  42. {
  43. window.location = jsonResult.RedirectToUrl;
  44. }
  45. }
  46. });
  47. });
  48. });
  49.  
  50. @if (String.IsNullOrWhiteSpace(Layout))
  51. {
  52. // Do somthing if it is partial view
  53. }
  54. else
  55. {
  56. // Do somthing if it is full page view
  57. }
Add Comment
Please, Sign In to add comment