2

I have read quite some blogs and stackoverflow answers on how to send a JavaScript object to an asp.net MVC controller. But all examples I have seen so far require you to know which properties the JavaScript object will have (because they all convert the JavaScript object to a C# object).

When I was using PageMethods in asp.net webforms I could send any complex JavaScript object or array (even hierarchical data) and it would be converted to a Dictionary which I could iterate. Any chance I can do something similar in asp.net MVC?

4 Answers 4

1

I now found a way which works for me.

  1. I am converting my data to json and receive it as a string in my ASP.net MVC controller.
  2. Then I use the json.net library to read the data - since this library allows me to read the data without converting it to a C# (or VB) object.

JavaScript code:

//first include the json2 library for older browsers and jQuery
$.post(url, {json: JSON.stringify(mydata)});

Server side code:

public void xyz(string json)
{
    JObject j = JObject.Parse(json);
    string name = (string)j.SelectToken("Products[0].Name");
    //...
}

The good thing: it is "normal" json which means it is not some uncommon format or interface-type.

EDIT: I found out that I don't even need the C# json-library if I am using .net 4 - since I can convert any json string to a Dictionary with the help of the JavaScriptSerializer:

JavaScriptSerializer jss = new JavaScriptSerializer();
Dictionary<string, object> data = (Dictionary<string, object>) jss.Deserialize<dynamic>(json_string);
Sign up to request clarification or add additional context in comments.

Comments

0

What you can do is use jQuery plugin toDictionary, this plugin will transform your object to dictionary that asp.net MVC default model binder can understand

e.g.

 $.ajax({
    url: "/SomeURL",
    type: "POST",
    data: $.toDictionary(dataToSend)
   });

Remember dataToSend is your object

2 Comments

Looks promising - but I'm not sure if it provides what I need, since I can't get it to work. I don't know what type to use for the parameter in the mvc controller. In the blog example he is only showing it with "predefined classes" (Person and Car) but I don't have such server-side classes since I don't know which properties the JavaScript object is going to have.
0

This will be converted to a dictionary:

[{"key":0, "value":"something"},{"key":2, "value":"something else"},]

Obviously, you could do string,string or int, bool or etc...

For example, I have a method like this:

public int CollabSortFolder(int FolderId, Dictionary<int, int> Items)

I would call is using a GET:

/CollabSortFolder?FolderId=111111&Items=[{"Key":3685,"Value":0},{"Key":3670,"Value":1},{"Key":3687,"Value":2}]

Now as a GET it's not very elegant, but a post would work the same way.

2 Comments

Unfortunately it's not working in my example - my parameter is always null. Could you tell me how the serverside controller action would look like. (which parameter?)
It's still not working for me (even your example code). Items is always null or just empty. I don't know what I'm doing wrong. I am using asp.net MVC 3.
0

Have you tried using the Request.Forms dictionary?

public ActionResult MyAction(FormCollection formValues)
{
}

Iterate over formValues

1 Comment

Yes, I tried that. But I found it very inconvenient when I have hierarchical data (which I have). Since everything will be a flat list.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.