0

I need to send an array of objects to asp.net mvc 2 through json, but i am not getting null in the mvc controller

The object is like this

entries[1].date = "12/22/2014"
entries[1].Ref = "0002"
entries[1].Credit = "100"

entries[2].date = "12/22/2014"
entries[2].Ref = "0002"
entries[2].Credit = "150"

later before send, i do this

JSON.stringify(entries)

My data in Json looks like this

[{"date":"12/22/2014","RefNo":"0002","Credit":"100"},{"date":"12/22/2014","RefNo":"0002","Credit":"150"}]

my controller in asp.net method looks like this

[HttpPost]
public ActionResult Save(MyDataModel[] entries)
{

}

and my jquery send code

 $.ajax({ // create an AJAX call...
      type: 'post', // GET or POST
        url: url, // the file to call
        dataType: 'json',
        data: JSON.stringify(entries), // get the form da
})

The MyDataModel is a model class with the properties of the entries

More data

 public class MyDataModel
{

    public string Date { get; set; }

    [Required(ErrorMessage = "RefNo is required", AllowEmptyStrings = false)]
    [StringLength(40, ErrorMessage = "RefNo Must be Under 40 characters long", MinimumLength = 1)]
    public string RefNo { get; set; }

    // Credit
    [Required(ErrorMessage = "Credit is required", AllowEmptyStrings = false)]
    [Range(typeof(Decimal), "1", "999999999999", ErrorMessage = "Credit Must be a number and non zero")]
    public decimal? Credit { get; set; }

}

But the entries is always null, why?

5
  • I think MyDataModel[] entries should be String entries because you send a json string. Commented Dec 22, 2014 at 15:35
  • 1
    @SinaSadrzadeh: No; the model binder can parse JSON. Commented Dec 22, 2014 at 15:36
  • 1
    Can we see MyDataModel. Commented Dec 22, 2014 at 15:36
  • Can you get the JSON string that is sent to the server? You could use the browser dev tools, or Fiddler, to get that data. Being able to see the JSON could help us figure out what's going wrong. Commented Dec 22, 2014 at 16:15
  • On the c# side, what serializer are you using - Json.NET, DataContractJsonSerializer or JavaScriptSerializer? Commented Dec 22, 2014 at 16:50

4 Answers 4

1

Your JSON looks incorrect after stringify?

[
{"date":"12/22/2014","RefNo":"0002","Credit":"100"},
{"date":"12/22/2014","RefNo":"0002","Credit":"150"

]

Missing closing brace..maybe a typo in question only but thought I'd let you know, may help!

Sign up to request clarification or add additional context in comments.

Comments

1
$.ajax({ // create an AJAX call...
      type: 'post', // GET or POST
        url: url, // the file to call
        contentType: 'application/json; charset=utf-8', // Include ContentType
        dataType: 'json',
        data: JSON.stringify(entries), // get the form da
})

You have to include contentType and It will work after that.

2 Comments

i am currently using this $.ajax({ // create an AJAX call... type: 'post', // GET or POST url: url, // the file to call traditional: true, dataType: 'json', contentType: 'application/json; charset=utf-8', data: data, // get the form data success: successFunc, error: errorFunc });, which is not also working
Try to do data : JSON.strigify(entries)
1

Maybe you need to include the contentType to the ajax call and if that does not work, you could try this way: data: JSON.stringify({"entries" : entries})

where the entries in quotes is the parameter on the controller and the entries without quotes is your object. And in your controller

[HttpPost]
public ActionResult Save(List<MyDatAModel> entries)
{

}

You are posting a complex object, so a list of your model will receive your array and add all the elements.

UPDATE Try this form (this is an example of many ajax request I use) maybe it could help.

           $.ajax({
                type: 'POST',
                url: '@Url.Action("Action", "Controller")',
                data: JSON.stringify(arrayJavascript),
                dataType: 'json',
                contentType: 'application/json; charset=utf-8',
                success: function () {
                  //code...
                }
           });

3 Comments

I tried the List<MyDataModel> too, but it still does not work
i already did what you added to the update too, check stackoverflow.com/a/27606097/362461
well the last thing that comes to my mind is the cache option in the ajax, in your comment you are not using cache, give it a try
0

change

data: JSON.stringify(entries), // get the form da

to

data: { entries : entries },

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.