okrupovich
okrupovich

Reputation: 45

How deserialize json List<string> data in JsonResult MVC

I'm trying deserialize json string to MyModel but get "Error" from my Ajax:

$.ajax({
                    type: 'POST',
                    traditional: true,
                    url: '/Employee/Extract',
                    data: {data: arrToServer},
                    dataType: 'json',
                    success: function (result) {
                        alert("good");
                    },
                    error: function(result) {
                        alert("Error");
                    }
                });

My json is:"[{\"CategoryId\":\"1\",\"EnteredDate\":\"15.02.2014\",\"SystemDate\":\"15.02.2014\",\"EmpId\":\"1\",\"ProductId\":\"9\"},{\"CategoryId\":\"1\",\"EnteredDate\":\"15.02.2014\",\"SystemDate\":\"15.02.2014\",\"EmpId\":\"1\",\"ProductId\":\"9\"},{\"CategoryId\":\"1\",\"EnteredDate\":\"15.02.2014\",\"SystemDate\":\"15.02.2014\",\"EmpId\":\"1\",\"ProductId\":\"9\"}]"

My JsonResult:

[HttpPost]
        public JsonResult Extract()
        {
List<string> myDeserializedObjList = (List<string>)Newtonsoft.Json.JsonConvert.DeserializeObject(Request["data"], typeof(List<string>));
            return Json("111");
        }
or get a "Good" when my JsonResult is empty:
public JsonResult Extract(List<string> data)
        {
            return Json("111");
        }

My model:

public class MyModel
{
    public int CategoryId { get; set; }
    public string EnteredDate { get; set; }
    public string SystenDate { get; set; }
    public int EmpId { get; set; }
    public int ProductId { get; set; }
}

Upvotes: 0

Views: 2228

Answers (3)

YK1
YK1

Reputation: 7612

You don't need to do the JSON de-serialization manually. The ASP.NET MVC runtime will do it for you. You will have to make a few corrections/changes:

First get your ajax call right. Define contentType and pass the data array correctly.

$.ajax({
                type: 'POST',
                traditional: true,
                url: '/Employee/Extract',
                data: JSON.stringify(arrToServer), // stringify array
                dataType: 'json',
                contentType: 'application/json',   // define content type
                success: function (result) {
                    alert("good");
                },
                error: function(result) {
                    alert("Error");
                }
            });

At your controller, you can simply get the data via parameters:

    [HttpPost]
    public JsonResult<string> Extract(MyModel[] modelArray)
    {
        // voila! , modelArray has contents you passed from JavaScript
        return Json("111");
    }

Upvotes: 0

alexmac
alexmac

Reputation: 19617

You have two errors in your code.

  1. Type of parameter in action should be List<MyModel>, instead List<string>.
  2. ContentType in jQuery.ajax should be application/json in your case.

By default jQuery.ajax contentType is application/x-www-form-urlencoded. This means that parameters sent in url string. You should change contentType in jQuery.ajax to application/json and convert your js object to json with JSON.stringify:

$.ajax({
    type: 'POST',
    traditional: true,
    url: '/Employee/Extract',
    contentType: 'application/json',
    dataType: 'json',
    data: JSON.stringify({data: arrToServer}),        
    success: function (result) {
        alert("good");
    },
    error: function(result) {
        alert("Error");
    }
});

Upvotes: 1

Ivan.Srb
Ivan.Srb

Reputation: 1851

You should use List<MyModel> instead of List<string>.

Upvotes: 0

Related Questions