Note that there are some explanatory texts on larger screens.

plurals
  1. POProblem passing JSON data using jQuery $.ajax() to .NET MVC Action w/ custom BindingModel
    primarykey
    data
    text
    <p>I am trying to pass JSON data from the client browser to an ASP.NET MVC Action using jQuery $.ajax() and bind it to a .NET class using a custom ModelBinder.</p> <p>THE CLIENT JAVASCRIPT:</p> <pre><code>$('#btnPatientSearch').click(function() { var patientFilter = { LastName: 'Flinstone', FirstName: 'Fred' }; var jsonData = $.toJSON(patientFilter); $.ajax({ url: '/Services/GetPatientList', type: 'GET', cache: false, data: jsonData, contentType: 'application/json; charset=utf-8', dataType: 'json', timeout: 10000, error: function() { alert('Error loading JSON=' + jsonData); }, success: function(jsonData) { $("#patientSearchList").fillSelect(jsonData); } }); </code></pre> <p>THE .NET CLASS FOR THE JSON DATA</p> <pre><code>[ModelBinder(typeof(JsonModelBinder))] public class PatientFilter { #region Properties public string IDNumber { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string SSN { get; set; } public DateTime DOB { get; set; } #endregion } </code></pre> <p>THE MVC ACTION</p> <pre><code> public JsonResult GetPatientList(iPatientDoc.Models.PatientFilter patientFilter) { </code></pre> <p>THE CUSTOM MODELBINDER</p> <pre><code>public class JsonModelBinder : IModelBinder { #region IModelBinder Members public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { if (controllerContext == null) throw new ArgumentNullException("controllerContext"); if (bindingContext == null) throw new ArgumentNullException("bindingContext"); var serializer = new DataContractJsonSerializer(bindingContext.ModelType); return serializer.ReadObject(controllerContext.HttpContext.Request.InputStream); #endregion } } </code></pre> <p>The custom ModelBinder is called correctly, yet the Request.InputStream is empty so there is no data to bind to the PatientFilter object.</p> <p>Any thoughts appreciated. Chris</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload