asp.net - Problem passing JSON data using jQuery $.ajax() to .NET MVC Action w/ custom BindingModel -


I am trying to use JSON data from ASP.NET MVC action from client browser using jquery $ .ajax () I am Tie this to a .NET class using a custom ModelBinder

Client JavaScript: .. Click on

  $ ('# btnPatientSearch') (function {} {var patientFilter = {last name: 'flinstone', first name: 'fred'}; json.data = $ .toJSON (PatientFilter); $ .ajax ({url: '/ service / GetPatientList', type: 'received', cache: false, data: JsonData, contentType: 'application / json; charset = utf-8', data type: 'Json', timeout: 10000, error: function () {warning (error loading JSON = '+ jsonData);}, success: function jsonData) {$ ("# patientSearchList") fillSelect (jsonData);}}} ;  

.NET class for JSON data

  [ModelBlinder (typef (JSONmodebanker)) Public class petit filter {#region properties public string IDNumber {get; Set;} publ ic string firstname {received; Set; } Public String LastName {get; Set; } Public string SSN {get; Set; } Public Date Time DOB (Receive); Set; } #MessageContent  

MVC action

  Public JsonResult GetPatientList (iPatientDoc.Models.PatientFilter patientFilter) { 

custom MODELBINDER

  public class JsonModelBinder: IModelBinder {#region IModelBinder member throw public object BindModel (ControllerContext controllerContext, ModelBindingContext bindingContext) {if (controllerContext == null) new ArgumentNullException ( "controllerContext"); If (binding contact == empty) remove the new logic ("Binding Contains"); Var Serializer = New Data ContrastJason Serializer (Binding Contax Model Type); Return Serializer. ReadObject (Controller Consort ..httcx RequestInststream); #endregion}}  

Custom ModelBinder no data is correctly stated, however Request.InputStream is left blank, to force PatientFilter object.

Any thoughts appreciated. Chris

Some thoughts on this

  • You use a GET request I think the request body is always empty for GET
  • Your Patient Filter class has no [DataContract] attribute. I'm not sure that it will do anything serial
  • I'll have your $. Ajax () is not sure about the call. I expected that the data option would take an object instead of just a JSON string. After watching, I will try to set the process data option incorrectly.

There is also an interesting explanation for the data option:

The data must be sent to the server. This query is converted to a string, if there is no string already. See processData options to prevent this automatic processing has been added to the URL for GET-requests. The object key / value must be added. If the value is an array, then sorting multiple values ​​with the same value of jQuery. {Foo: ["bar1", "bar2"]} '& amp; Foo = bar1and Foo = bar2 '.


Comments

Popular posts from this blog

c++ - Linux and clipboard -

What is expire header and how to achive them in ASP.NET and PHP? -

sql server - How can I determine which of my SQL 2005 statistics are unused? -