Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 29th, 2012  |  syntax: None  |  size: 2.45 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. HTTP Post request from different controller actions and ModelState
  2. public class ProductController : Controller {
  3.  
  4.     [HttpPost]
  5.     public ActionResult Search(SearchModel searchModel) {
  6.  
  7.         if (ModelState.IsValid) {
  8.             //Do some stuff...
  9.  
  10.             return RedirectToAction("Index", "SearchResult");
  11.         }
  12.         return View(searchModel);
  13.     }
  14. }
  15.        
  16. public class SearchModel {
  17.  
  18.     [Required]
  19.     public string ProductCategory { get; set; }
  20.  
  21.     [Required]
  22.     public string ProductName { get; set; }
  23. }
  24.        
  25. @model MvcApplication20.SearchModel
  26.  
  27. @using (Html.BeginForm("search", "product"))
  28. {
  29.     @Html.EditorForModel()
  30.  
  31.     <input type="submit" value="Search" />
  32. }
  33.        
  34. @{
  35.     ViewBag.Title = "Home Page";
  36. }
  37.  
  38. <h2>@ViewBag.Message</h2>
  39.  
  40. @Html.Partial("_SearchPartialView")
  41.        
  42. [HttpPost]
  43. public ActionResult Search(SearchModel searchModel) {
  44.  
  45.     if (ModelState.IsValid) {
  46.         //Do some stuff...
  47.  
  48.         return RedirectToAction("Index", "SearchResult");
  49.     }
  50.     // since we are returning a view instead of a partial view,
  51.     // the _SearchPartialView template should be displayed with the layout
  52.     return View("_SearchPartialView", searchModel);
  53. }
  54.        
  55. $(document).on('submit', '#searchForm', function() {
  56.     $.ajax({
  57.         url: this.action,
  58.         type: this.method,
  59.         data: $(this).serialize(),
  60.         success: function(result) {
  61.             if (result.redirectTo) {
  62.                 // no validation errors we can redirect now:
  63.                 window.location.href = result.redirectTo;
  64.             } else {
  65.                 // there were validation errors, refresh the partial to show them
  66.                 $('#searchContainer').html(result);
  67.  
  68.                 // if you want to enable client side validation
  69.                 // with jquery unobtrusive validate for this search form
  70.                 // don't forget to call the .parse method here
  71.                 // since we are updating the DOM dynamically and we
  72.                 // need to reattach client side validators to the new elements:
  73.                 // $.validator.unobtrusive.parse(result);
  74.             }
  75.         }
  76.     });
  77.     return false;
  78. });
  79.        
  80. <div id="searchContainer">
  81.     @Html.Partial("_SearchPartialView")
  82. </div>
  83.        
  84. [HttpPost]
  85. public ActionResult Search(SearchModel searchModel) {
  86.  
  87.     if (ModelState.IsValid) {
  88.         //Do some stuff...
  89.  
  90.         return Json(new { redirectTo = Url.Action("Index", "SearchResult") });
  91.     }
  92.     return PartialView("_SearchPartialView", searchModel);
  93. }