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

Untitled

By: a guest on May 1st, 2012  |  syntax: None  |  size: 3.79 KB  |  hits: 37  |  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. MVC3 Controller null parameter using json.stringify
  2. public JsonResult GetById(Guid id)
  3. {
  4.     var results = from a in repository.AsQueryable<Department>()
  5.                   where a.Id == id
  6.                   orderby a.Name
  7.                   select new { id = a.Id, name = a.Name };
  8.  
  9.     return Json(results, JsonRequestBehavior.AllowGet);
  10. }
  11.        
  12. $(document).ready(function () {
  13.     var o = new Object();
  14.     o.id = 'C21803C3-1385-462E-ACEA-AFA1E554C635';
  15.  
  16.     $.getJSON('@Url.Action("GetById", "User")', JSON.stringify(o), function () {
  17.         alert('Completed');
  18.     });
  19. });
  20.        
  21. $(document).ready(function () {
  22.     $.getJSON('@Url.Action("GetById", "User")', { "id": "C21803C3-1385-462E-ACEA-AFA1E554C635" }, function () {
  23.         alert('Completed');
  24.     })
  25.     .error(function (a, b, c) {
  26.         alert(a.responseText); alert(b); alert(c);
  27.     });
  28. });
  29.        
  30. $(document).ready(function () {
  31.     var o = new Object();
  32.     o.id = 'C21803C3-1385-462E-ACEA-AFA1E554C635';
  33.  
  34.     alert(JSON.stringify(o));
  35. });
  36.        
  37. {"id":"C21803C3-1385-462E-ACEA-AFA1E554C635"}
  38.        
  39. $(document).ready(function () {
  40.     var o = new Object();
  41.     o.id = 'C21803C3-1385-462E-ACEA-AFA1E554C635';
  42.  
  43.     var json_text = JSON.stringify(o, null, 2);
  44.     alert(json_text);
  45.  
  46.     var your_object = JSON.parse(json_text);
  47.  
  48.     alert(your_object.id);
  49. });
  50.        
  51. C21803C3-1385-462E-ACEA-AFA1E554C635
  52.        
  53. $.ajaxSetup({ cache: false });
  54.  
  55. $(document).ready(function () {
  56.     $(".openDialog").live("click", function (e) {
  57.         e.preventDefault();
  58.  
  59.         $("<div></div>")
  60.                 .addClass("dialog")
  61.                 .attr("id", $(this).attr("data-dialog-id"))
  62.                 .appendTo("body")
  63.                 .dialog({
  64.                     title: $(this).attr("data-dialog-title"),
  65.                     close: function () { $(this).remove() },
  66.                     modal: true
  67.                 })
  68.                 .load(this.href);
  69.     });
  70.  
  71.     $(".close").live("click", function (e) {
  72.         e.preventDefault();
  73.         $(this).closest(".dialog").dialog("close");
  74.     });
  75.  
  76.  
  77.  
  78.     var clickableTable = $('tr[data-tr-clickable-url]');
  79.  
  80.     if (clickableTable.length > 0) {
  81.         clickableTable.addClass('clickable')   // Add the clickable class for mouse over
  82.             .click(function () {
  83.                 window.location.href = $(this).attr('data-tr-clickable-url');
  84.             });
  85.  
  86.         // Remove the last child, containing anchors to actions, from each row, including the header.
  87.         $('tr :last-child').remove();
  88.     }
  89. });
  90.        
  91. var o = new Object();
  92. o.Id = 'C21803C3-1385-462E-ACEA-AFA1E554C635';
  93.  
  94. $.ajax({
  95.     url: '@Url.Action("GetById", "User")',
  96.     type: "POST",
  97.     data: JSON.stringify(o),
  98.     dataType: "json",
  99.     contentType: "application/json; charset=utf-8",
  100.     success: function () {
  101.         alert('completed');
  102.     }
  103. });
  104.        
  105. var o = new Object();
  106. o.Id = 'C21803C3-1385-462E-ACEA-AFA1E554C635';
  107.  
  108. $.ajax({
  109.     url: '@Url.Action("GetById", "User")',
  110.     data: JSON.stringify(o),
  111.     dataType: "json",
  112.     contentType: "application/json; charset=utf-8",
  113.     success: function () {
  114.         alert('completed');
  115.     }
  116. });
  117.        
  118. $.ajax({
  119.   url: url,
  120.   dataType: 'json',
  121.   data: data,
  122.   success: callback
  123. });
  124.        
  125. $.getJSON('/', JSON.stringify({id:"test"}));
  126.        
  127. $.getJSON('/', {id:"test"});
  128.        
  129. public class Binder : IModelBinder
  130. {
  131.     #region IModelBinder Members
  132.  
  133.     public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
  134.     {
  135.         var query = controllerContext.HttpContext.Request.Url.Query;
  136.         var json = System.Web.HttpUtility.UrlDecode(query.Remove(0,1));
  137.         JavaScriptSerializer serializer = new JavaScriptSerializer();
  138.         return serializer.Deserialize(json, bindingContext.ModelType.GetType());
  139.     }
  140.  
  141.     #endregion
  142. }
  143.        
  144. $.getJSON('@Url.Action("GetById", "User")', JSON.stringify(o.id), function () {
  145.     alert('Completed');
  146. });