0

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; }
}
1
  • return Json("111"); wont return valid json Commented Feb 15, 2014 at 18:55

3 Answers 3

1

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");
    }
});
Sign up to request clarification or add additional context in comments.

2 Comments

"thanks", I got empId, but I changed code from JsonResult to ActionResult, why ajax return "Error"? My new code: [HttpPost] public ActionResult Extract() { List<MyModel> myDeserializedObjList = (List<MyModel>)Newtonsoft.Json.JsonConvert.DeserializeObject(Request["data"], typeof(List<MyModel>)); foreach (MyModel model in myDeserializedObjList) { int empId = model.EmpId; ViewBag.empID = empId; } return RedirectToAction("Index"); }
@okrupovich because, in your code, jQuery.ajax expect json result (dataType: 'json'), but in action your return html. If you return html, you should change dataType to 'html'.
0

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

2 Comments

if I'm using List<MyModel> - data Count = 0
Try to use data: arrToServer instead of data: {data: arrToServer} in your Ajax request configuration. Also, I'm not very familiar with Newtonsoft.Json, so try it with [HttpPost] public JsonResult Extract(List<MyModel> data) { ... }
0

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");
    }

Comments

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.