0

I want to post JSON array to MVC controller through AJAX POST as:

 $.ajax({
 type: 'POST',
 data: json.stringify(totaldata),
 traditional:true,
 url: '/Builder/Save',
 success: function () {
 alert("Playlist saved successfully!!");
 }
 })

and my controller code is have made array of one ViewModel & want to update those values as.

[HttpPost]
 public ActionResult Save(IList<ItemEditViewModel> data,long playlistid=0, string Title="")
 {
 for (int i = 0; i < data.Count; i++)
 {
 var pc = db.PlaylistContents.FirstOrDefault(x => x.PlaylistContentId == data[i].ID);
 if (pc == null)
 {
 pc = new PlaylistContent();
 db.PlaylistContents.Add(pc);
 }
 pc.ContentMetaDataId = data[i].MetaID;
 pc.PlaylistContentSequenceId = i + 1;
 }
 db.SaveChanges();
 return RedirectToAction("Playlist", new {ID=playlistid });
}

But the Object is set to null in Controller.

My ViewModel is as,

public class ItemViewModel
{
 public long ID{get;set;}
 public long MetaID{get;set;}
}
asked May 4, 2013 at 7:21

3 Answers 3

3

Try adding the content type in ajax call,

contentType : 'application/json',

By default ajax sends with content type application/x-www-form-urlencoded; charset=UTF-8

Edit

also i dont know it this is a typo, json.stringify should be JSON.stringify

hope this helps.

answered May 4, 2013 at 11:58

Comments

0
 $.ajax({
 type: 'POST',
 data: {"data":json.stringify(totaldata)},//You missed the format
 traditional:true,
 url: '/Builder/Save',
 success: function () {
 alert("Playlist saved successfully!!");
 }
 })
answered May 4, 2013 at 8:05

1 Comment

Its not the Issue with the Formating
-2

The Problem is solved & my application is working now properly. I have just done JSON.stringify() on array elements & not the whole ajax data for posting.

the code is as written below:

 var totaldata = { data: data, playlistid: parseInt(playlistid), Title: Title };
 $.ajax({
 type: 'POST',
 data: { data: JSON.stringify(data), playlistid: parseInt(playlistid), Title: Title, deleted: JSON.stringify(deleted) },
 traditional:true,
 url: 'Save',
 success: function (data) {
 alert("Playlist saved successfully!!");
 }
 })
answered May 7, 2013 at 6:17

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.