Want to Save Posted Json data to database

I am posting JSON data through AJAX POST as ::

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

the JSON data is sent in following format::

{"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"}:

and as I made an ItemViewModel as ::

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

and My controller code is as::

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

while Execution of data in controller it doesn't accept the POSTED data as its ViewModel values.

Answers


My Problem is solved. As I have Posted array in combination with two other variables, I just need to stringify array & not the whole ajax data which is to be posted. as you can see in my code 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!!");

                    }
                })

In above code I have just done stringify on array & not on other data.

Thanks for some Co-operations of yours.


Need Your Help

6th degree curve fitting with numpy/scipy

python numpy scipy curve-fitting linear-regression

I have a very specific requirement for interpolating nonlinear data using a 6th degree polynomial. I've seen numpy/scipy routines (scipy.interpolate.InterpolatedUnivariateSpline) that allow interpo...

Can you make hovered state in Firebug “sticky?”

html css firefox firebug hover

When I'm debugging a site, sometimes the hovered selectors are a little long winded and similar to other ones, is there a way to apply a kind of "sticky" state to hover rules in Firebug?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.