Saving the MVVM model

4 posts, 1 answers
  1. Steve
    Steve avatar
    1851 posts
    Member since:
    Dec 2008

    Posted 01 Apr 2012 Link to this post

    Can someone tell me whats the best way to send a rather complex MVVM object up through an $.ajax call to be saved?

    ...this?

    data: "{'data':'" +JSON.stringify(_mydata) +"'}",
  2. Answer
    Randy
    Randy avatar
    72 posts
    Member since:
    Jan 2009

    Posted 01 Apr 2012 Link to this post

    I am no expert on this, just got it working a few days ago, but here is the code I use.

    I had two problems, I had to use a POST dues to the complexity of the data being sent and I had to deal with dates. On read I moved the dates to a shadow field and then on save I moved them back, converting to and from WCF / Javascript Dates.

    $(".clickSave").click(function (e) {
        tg.viewModel.data.ApplicationCreatedOn = dateToWcf(tg.viewModel.data.shadowApplicationCreatedOn);
        tg.viewModel.data.LastUpdatedOn = dateToWcf(tg.viewModel.data.shadowLastUpdatedOn);
        tg.viewModel.data.DeclarationDateTime = dateToWcf(tg.viewModel.data.shadowDeclarationDateTime);
        tg.viewModel.data.ApplicantDob = dateToWcf(tg.viewModel.data.shadowApplicantDob);
        tg.viewModel.data.IsDirty = true;
     
        $.ajax({
            type: 'POST',
            contentType: "application/json; charset=utf-8",
            url: tg.crudServiceBaseUrl + "/DDCApplicationSave",
            data: JSON.stringify(tg.viewModel.data),
            processData: true,
            dataType: "json",
            success: function (data) {
                if (data == "") {
                    toast("You application as been saved to our servers.");
                } else {
                    alert("An error occured while saving the application. \n\n The error was: " + data + "\n\n If you close your browser now, any unsaved data will be lost.");
                };
            }
        });
    });


    Hope this helps
    Randy
  3. Kendo UI is VS 2017 Ready
  4. Steve
    Steve avatar
    1851 posts
    Member since:
    Dec 2008

    Posted 01 Apr 2012 Link to this post

    Ahha, okay thanks...I think I can run with that :)
  5. axwack
    axwack avatar
    37 posts
    Member since:
    Aug 2012

    Posted 24 Aug 2012 Link to this post

    I have been using the datasource and there is a parameterMap that can do the data manipulation for you. If you go the BLOGS there is an example of using it in the CRUD demo.

    What I'm struggling with is how to set data that has a function associated with it. 
Back to Top
Kendo UI is VS 2017 Ready