Set model as dirty from AJAX

11 posts, 0 answers
  1. Jonas
    Jonas avatar
    39 posts
    Member since:
    Oct 2014

    Posted 12 Nov 2015 Link to this post

    Hello 

    Within my editor for a scheduler I'm changing some values through an AJAX-call and apparantely this does not change the model since the update action is never fired when I press update. The update works fine if I change a model bound value. How can I manually set the model as dirty?

    Do I do it in the success method for the AJAX call and how?

    Basically I just need to trigger update wheter the model is changed or not.

    /Jonas

  2. Jonas
    Jonas avatar
    39 posts
    Member since:
    Oct 2014

    Posted 16 Nov 2015 Link to this post

    No one has any idea?
  3. UI for ASP.NET MVC is VS 2017 Ready
  4. Georgi Krustev
    Admin
    Georgi Krustev avatar
    3707 posts

    Posted 16 Nov 2015 Link to this post

    Hello Jonas,

    The SchedulerEvent model will be marked as "dirty" if the model is updated using its set method. Thus the datasource will know that those models should be updated/created/deleted. 
    In order to assist you further, please send us a repro demo that shows your current approach. Thus we will be able to review it and advice you further.

    Regards,
    Georgi Krustev
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  5. Jonas
    Jonas avatar
    39 posts
    Member since:
    Oct 2014

    Posted 16 Nov 2015 in reply to Georgi Krustev Link to this post

    Hello Georgi

     From my edit template I'm making an AJAX call to do some stuff and in the success ​method I added the set method and I can see that a value gets changed but the .Update is still not triggered when I press update.

    $.ajax({
                url: "Home/updateBooking/",
                data: {
                    TaskID: $("#TaskID").val(),
                    personId: $(this).attr('name'),
                    bookingType: $(this).attr('bookingtype'),
                    taskOperators: $('#taskOperators').val()
                },
                cache: false,
                type: "POST",
                dataType: "JSON",
                context: this,
                error: function (request, statusText, errorMessage) {
                    $('#flash-message-error').show();
                    $('#flash-message-error').fadeOut(3000);
                },
                success: function (data, textStatus, XMLHttpRequest) {
     
                    $('#taskOperators').val(data.taskOperators);
                    //Do some UI stuff
                    var observable = new kendo.data.ObservableObject({ isChanged: false });
                    alert(observable.get("isChanged"));
                    observable.set("isChanged", true); // set the value
                    alert(observable.get("isChanged"));
     
                }
            });

    Is this the correct place to do it, or should I do it server side in the called action  Home/updateBooking/?

  6. Jonas
    Jonas avatar
    39 posts
    Member since:
    Oct 2014

    Posted 16 Nov 2015 in reply to Georgi Krustev Link to this post

    Hello

    From within my edittemplate I'm making an AJAX call and in the sussess method I have tried to update the model using the set method, I can see that the value gets changed but there is still no call to my .update action when I press the update button. Here is the code for my AJAX call:

    $.ajax({
                url: "Home/updateBooking/",
                data: {
                    TaskID: $("#TaskID").val(),
                    personId: $(this).attr('name'),
                    bookingType: $(this).attr('bookingtype'),
                    taskOperators: $('#taskOperators').val()
                },
                cache: false,
                type: "POST",
                dataType: "JSON",
                context: this,
                error: function (request, statusText, errorMessage) {
                    $('#flash-message-error').show();
                    $('#flash-message-error').fadeOut(3000);
                },
                success: function (data, textStatus, XMLHttpRequest) {
     
                    $('#taskOperators').val(data.taskOperators);
                    //doing some UI stuff...
     
                    var observable = new kendo.data.ObservableObject({ isChanged: false });
                    alert(observable.get("isChanged"));
                    observable.set("isChanged", true); // set the value
                    alert(observable.get("isChanged"));
                }
            });

    Should I do it in the Success method or elsewhere?
    Is there another way to force the update action to run? a custom update?

     

     

  7. Jonas
    Jonas avatar
    39 posts
    Member since:
    Oct 2014

    Posted 16 Nov 2015 in reply to Jonas Link to this post

    sorry about the double posts, I got an error message when I first tried to post and thought it didn't get through
  8. Georgi Krustev
    Admin
    Georgi Krustev avatar
    3707 posts

    Posted 18 Nov 2015 Link to this post

    Hello Jonas,

    Probably, I am missing something because the "set method" approach works fine for me. Check this Dojo demo for more details:
    Could you modify it and demonstrate the erroneous behavior?

    Regards,
    Georgi Krustev
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  9. Jonas
    Jonas avatar
    39 posts
    Member since:
    Oct 2014

    Posted 18 Nov 2015 in reply to Georgi Krustev Link to this post

    Yes, the set method updates my hidden value correct, but this still doesn't trigger the update action, the form just closes when I press update and the task_Update action isn't triggered.

    this is the hidden value in my viewmodel:

    public bool isChanged { get; set; }

    added to the form, like this:

    @Html.HiddenFor(model => model.isChanged, new { data_value_primitive = "true" })

    and updated by AJAX like this:

    success: function (data, textStatus, XMLHttpRequest) {
                    //Other stuff removed to focus on the set method
                    var observable = new kendo.data.ObservableObject({ isChanged: false });
                    observable.set("isChanged", true); // set the value
                }

    Is there a way to set the model as dirty in the action called by the AJAX case or is there a way to always trigger the update action for the update button regardless off if the model is "dirty" or nor?

    This is the action I want triggered when I press update from the form regardless of if the model is seen as changed or not

    .Update(update => update.Action("Task_Update", "Home").Data("additionalInfo"))
     

     

  10. Vladimir Iliev
    Admin
    Vladimir Iliev avatar
    2172 posts

    Posted 20 Nov 2015 Link to this post

    Hello Jonas,

    From the provided code it seems that you still trying to create new observable object and update it's fields - please note that the Scheduler does not know about this object and if you actually need to update the event object than you should use the approach shown by my colleague Georgi. If you experience difficulties implementing the suggested approach in your real project you can provide runnable example where the described behavior can be reproduced and I would be happy to advice you how to proceed.

    Regards,
    Vladimir Iliev
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  11. Jonas
    Jonas avatar
    39 posts
    Member since:
    Oct 2014

    Posted 23 Nov 2015 in reply to Vladimir Iliev Link to this post

    Ok

    I will have to take a closer look at this later on.

    In the mean while as a work around, is there away to always trigger the update action?

    I.e

    .Update(update => update.Action("Task_Update", "Home").Data("additionalInfo"))

    How can I make sure this call always gets made?
    Can I override the built in update and write my own call to task_Update and how?

    /Jonas

     

  12. Vladimir Iliev
    Admin
    Vladimir Iliev avatar
    2172 posts

    Posted 24 Nov 2015 Link to this post

    Hi Jonas,

    This behavior is not supported and it will require custom code - for example you can try using the Scheduler and it's DataSource events to make sure currently edited event have dirty flag set to "true".

    Regards,
    Vladimir Iliev
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top
UI for ASP.NET MVC is VS 2017 Ready