简体   繁体   English

json数组发布到mvc控制器

[英]json Array posting to mvc controller

I want to post JSON array to MVC controller through AJAX POST as: 我想通过AJAX POST将JSON数组发布到MVC控制器,如下所示:

 $.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. 并且我的控制器代码已制成一个ViewModel数组,并希望将其更新为。

[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. 但是在Controller中将Object设置为null。

My ViewModel is as, 我的ViewModel是,

public class ItemViewModel
{
    public long ID{get;set;}
    public long MetaID{get;set;}
}

Try adding the content type in ajax call, 尝试在ajax调用中添加内容类型,

contentType : 'application/json',

By default ajax sends with content type application/x-www-form-urlencoded; charset=UTF-8 默认情况下,ajax发送的内容类型为application/x-www-form-urlencoded; charset=UTF-8 application/x-www-form-urlencoded; charset=UTF-8

Edit 编辑

also i dont know it this is a typo, json.stringify should be JSON.stringify 我也不知道这是一个错字, json.stringify应该是JSON.stringify

hope this helps. 希望这可以帮助。

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

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. 我刚刚对数组元素而不是整个ajax数据进行了JSON.stringify()的发布。

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

                    }
                })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM