views:

197

answers:

1

Background:

  1. I have a MVC2 project and am using jQuery 1.4.2.
  2. I have a .NET class that I put into a view using a JsonResult.
  3. The page then uses jQuery to do stuff which will result in properties of the object put on the page by item (2) above.
  4. When a certain page element is clicked jQuery $.post's the JSON.stringify(myObj) back to action method.

What I'd like to be able to have is something like:

[HttpPost] public ViewResult MyAction(MyClass minime) { }

... MyClass would be the .NET class to deserialize the JSON into which should be fine given it was the type MyClass that got "return Json(MyClass);"

At the present time what I'm seeing is that either the object parameter is null or has no values as set by the JS/jQuery code. If I try:

MyClass foo = new MyClass(); UpdateModel(foo);

It doesn't throw an exception but similar doesn't populate the class either.

Anybody have any ideas on how to solve this? Or how to deal with JSON sent back to the action method and get it into a class.

+1  A: 

If you have an Action

[HttpPost]
public JsonResult MyAction(MyClass minime)

where for example

public class MyClass {
    public string Name { get; set; }
    public int Age { get; set; }
}

you should fill all properties of the class MyClass as a parameters. The input data must not be in JSON format:

$.ajax({
    url: '/MyController/MyAction',
    data: { name: "Peter", age: 33 },
    dataType: 'json',
    traditional: true,
    type: 'POST',
    success: function (data) {
        // get data which are already deserialized from JSON
    }
});

If you have DateTime properties read http://stackoverflow.com/questions/3583252/json-date-parameter-passed-to-mvc-action-is-always-null/3583579#3583579.

Oleg
I'll look into that to see if that works. The main issue would be that my class in .NET has an array of classes like:class Myclass { public int Id {get; set;} public int SubClass[] Subclasses { get;set;}}class SubClass { public string Name { get; set;} public int[] Values { get; set; }}Each of the SubClass' could have a Values array of a different length. Therefore the JS needs to post stuff which is not so simple. Any ideas how I'd post something so complex as that.
Peter
MVC Actions are designed to be used as input of Forms. Forms have always only named properities. If you want to transfer pure data to the web server you should better create a WCF or ASMX service returnes JSON which can be a part of the same web site. Other way is declaring a method with a `string` parameter `minime` and use `data: {minime: JSON.stringify(minime)}` and `contentType: application/json` as a `jQuery.ajax` parameter and use `JavaScriptSerializer ser = new JavaScriptSerializer(); MyClass inst = ser.Deserialize<MyClass>(minime);` inside of `MyAction` to convert input string.
Oleg
`contentType` paremeter should be `contentType: "application/json; charset=utf-8"`
Oleg
Excellent Oleg!!!The JSON.stringify and JavaScriptSerializer() class idea did the trick and worked handsomely.
Peter