How to pass a JSON object to MVC action parameters?

I would like to convey

{"id":1, "name":"stackoverflow", "parameter2":false, "parameter3":true}

To my action

public JsonResult Action(int id, string name, bool parameter2, bool parameter3)
{
    //...
}

Using the jQueries ajax method using JSON as a data parameter

$.ajax({
   url: "target.aspx",
   data:  {"id":1, "name":"stackoverflow", "parameter2":false, "parameter3":true},
   success: handleResponse
});

I see that in fiddler my JSON object is dispatched, but they are not tied to my action parameters. How to make them bind to parameters?

I do not want to bind an object to an action that contains my values, i.e. I do not want Action (MyCustomObjectToAcceptParameters json). I want every JSON property to be associated with every action parameter.

querystring, , JSON /, , - json . datamembers, , ints boolean.

+5
3

, @womp , . JSON data​​strong > . . .

$.ajax({
   url: "target.aspx",
   data: {parameter1: true, parameter2: false, parameter3: true},
   success: handleResponse
 });
+2

The binder / filter is excellent. For more precise control over the display of json names ↔ of business class properties, use the [DataMember] attribute for properties and [DataContract] for the class. See DataContractJsonSerializer .

0
source

All Articles