想要将Json数据保存到数据库中

时间:2013-05-02 06:58:38

标签: asp.net-mvc json jquery asp.net-mvc-4

我通过AJAX POST发布JSON数据::

var length = $('.organizer-media').length;
                var contents = $('.organizer-media');

                var  Title;
                type == "New" ? Title = $('#newName').val() : Title = $('#Playlist option:selected').text();

                var playlistid = $('#Playlist').val()
                type == "New" ? playlistid = 0 : playlistid = $('#Playlist').val()

                var data = [];

                for (var i = 0; i < length; i++) {
                    data[i] = { ID: parseInt(contents[i].id), MetaID: parseInt(contents[i].title) }
                }
               var totaldata={ data: data, playlistid: parseInt(playlistid),Title:Title };    
                $.ajax({
                    type: 'POST',
                    data: JSON.Stringify(totaldata),
                    url: '/Builder/Save',
                    success: function () {
                        alert("Playlist saved successfully!!");
                    }
                })

JSON数据以下列格式发送::

{"data":[{"ID":779389,"MetaID":774367},{"ID":779390,"MetaID":774367},{"ID":779387,"MetaID":774366},{"ID":779388,"MetaID":774366},{"ID":779386,"MetaID":774365},{"ID":779385,"MetaID":774364},{"ID":779384,"MetaID":774363},{"ID":779383,"MetaID":774362},{"ID":779382,"MetaID":774361},{"ID":779381,"MetaID":774360},{"ID":779378,"MetaID":774359},{"ID":779379,"MetaID":774359},{"ID":779377,"MetaID":774358},{"ID":779376,"MetaID":774357},{"ID":779375,"MetaID":774356},{"ID":779372,"MetaID":774355},{"ID":779373,"MetaID":774355},{"ID":779374,"MetaID":774354},{"ID":779370,"MetaID":774353},{"ID":779394,"MetaID":774370}],"playlistid":1461,"Title":"MX OPEN TALENT MARIA"}:

并且我将ItemViewModel设为::

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

和我的控制器代码为::

[HttpPost]
        public ActionResult Save(ItemEditViewModel[] data,long playlistid=0, string Title="")
        {



            for (int i = 0; i < data.Length; 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 Json(new { foo = "bar", baz = "Blech" });
        }

在控制器中执行数据时,它不接受POSTED数据作为其ViewModel值。

1 个答案:

答案 0 :(得分:0)

我的问题解决了。 由于我已经将数组与其他两个变量结合使用,我只需要将数组和数组进行字符串化。不是要发布的整个ajax数据。 正如您在下面的代码中看到的那样::

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

                    }
                })

在上面的代码中我刚刚在array&amp;上完成了stringify。不在其他数据上。

感谢您的一些合作。