- MVC3 Controller null parameter using json.stringify
- public JsonResult GetById(Guid id)
- {
- var results = from a in repository.AsQueryable<Department>()
- where a.Id == id
- orderby a.Name
- select new { id = a.Id, name = a.Name };
- return Json(results, JsonRequestBehavior.AllowGet);
- }
- $(document).ready(function () {
- var o = new Object();
- o.id = 'C21803C3-1385-462E-ACEA-AFA1E554C635';
- $.getJSON('@Url.Action("GetById", "User")', JSON.stringify(o), function () {
- alert('Completed');
- });
- });
- $(document).ready(function () {
- $.getJSON('@Url.Action("GetById", "User")', { "id": "C21803C3-1385-462E-ACEA-AFA1E554C635" }, function () {
- alert('Completed');
- })
- .error(function (a, b, c) {
- alert(a.responseText); alert(b); alert(c);
- });
- });
- $(document).ready(function () {
- var o = new Object();
- o.id = 'C21803C3-1385-462E-ACEA-AFA1E554C635';
- alert(JSON.stringify(o));
- });
- {"id":"C21803C3-1385-462E-ACEA-AFA1E554C635"}
- $(document).ready(function () {
- var o = new Object();
- o.id = 'C21803C3-1385-462E-ACEA-AFA1E554C635';
- var json_text = JSON.stringify(o, null, 2);
- alert(json_text);
- var your_object = JSON.parse(json_text);
- alert(your_object.id);
- });
- C21803C3-1385-462E-ACEA-AFA1E554C635
- $.ajaxSetup({ cache: false });
- $(document).ready(function () {
- $(".openDialog").live("click", function (e) {
- e.preventDefault();
- $("<div></div>")
- .addClass("dialog")
- .attr("id", $(this).attr("data-dialog-id"))
- .appendTo("body")
- .dialog({
- title: $(this).attr("data-dialog-title"),
- close: function () { $(this).remove() },
- modal: true
- })
- .load(this.href);
- });
- $(".close").live("click", function (e) {
- e.preventDefault();
- $(this).closest(".dialog").dialog("close");
- });
- var clickableTable = $('tr[data-tr-clickable-url]');
- if (clickableTable.length > 0) {
- clickableTable.addClass('clickable') // Add the clickable class for mouse over
- .click(function () {
- window.location.href = $(this).attr('data-tr-clickable-url');
- });
- // Remove the last child, containing anchors to actions, from each row, including the header.
- $('tr :last-child').remove();
- }
- });
- var o = new Object();
- o.Id = 'C21803C3-1385-462E-ACEA-AFA1E554C635';
- $.ajax({
- url: '@Url.Action("GetById", "User")',
- type: "POST",
- data: JSON.stringify(o),
- dataType: "json",
- contentType: "application/json; charset=utf-8",
- success: function () {
- alert('completed');
- }
- });
- var o = new Object();
- o.Id = 'C21803C3-1385-462E-ACEA-AFA1E554C635';
- $.ajax({
- url: '@Url.Action("GetById", "User")',
- data: JSON.stringify(o),
- dataType: "json",
- contentType: "application/json; charset=utf-8",
- success: function () {
- alert('completed');
- }
- });
- $.ajax({
- url: url,
- dataType: 'json',
- data: data,
- success: callback
- });
- $.getJSON('/', JSON.stringify({id:"test"}));
- $.getJSON('/', {id:"test"});
- public class Binder : IModelBinder
- {
- #region IModelBinder Members
- public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
- {
- var query = controllerContext.HttpContext.Request.Url.Query;
- var json = System.Web.HttpUtility.UrlDecode(query.Remove(0,1));
- JavaScriptSerializer serializer = new JavaScriptSerializer();
- return serializer.Deserialize(json, bindingContext.ModelType.GetType());
- }
- #endregion
- }
- $.getJSON('@Url.Action("GetById", "User")', JSON.stringify(o.id), function () {
- alert('Completed');
- });