File #gkmecu34-6788 - CSHARP - Sourcecode

Uploaded by an anonymous user - 14/02/2012 22:14 - 384 Views
Source code
  1. //server side
  2.  
  3.     public class TestViewModel
  4.     {
  5.         public List<SelectListItem> AllTestItems { get; set; }
  6.         public List<string> SelectedTestItems { get; set; }
  7.     }
  8.  
  9.     public ActionResult TestAction(TestViewModel vm)
  10.     {
  11.         return null;
  12.     }
  13.  
  14.  
  15. //and the client side
  16.  
  17.  
  18. <script type="text/javascript">
  19.     $(document).ready(function () {
  20.         $("#btnSave").click(function () {
  21.             $.post('@Url.Action("TestAction")', $("#testDiv").serializeAnything(), function () {
  22.  
  23.             }, "json");
  24.         });
  25.     });
  26. </script>
  27. <div id="testDiv">
  28.     @Html.ListBoxFor(x => x.SelectedTestItems, Model.AllTestItems)
  29. </div>
  30. <button type="button" id="btnSave">
  31.     Save
  32. </button>
  33.  
  34.  
  35. (function ($) {
  36.  
  37. 	$.fn.serializeAnything = function () {
  38. 		var toReturn = [];
  39. 		var els = $(this).find(':input').get();
  40.  
  41. 		$.each(els, function () {
  42. 			if (this.name && !this.disabled && (this.checked || /select|textarea/i.test(this.nodeName) || /text|hidden|password/i.test(this.type))) {
  43. 				var val = $(this).val();
  44. 				toReturn.push(encodeURIComponent(this.name) + "=" + encodeURIComponent(val));
  45. 			}
  46. 		});
  47. 		return toReturn.join("&").replace(/%20/g, "+");
  48. 	}
  49. })(jQuery);