Results 1 to 7 of 7

Thread: ast.net MVC1 - strange behavior when calling actions.

  1. #1

    Thread Starter
    Fanatic Member stlaural's Avatar
    Join Date
    Sep 2007
    Location
    Quebec, Canada
    Posts
    683

    ast.net MVC1 - strange behavior when calling actions.

    Ok, this one is one of the strangest bug/behavior I've seen, I can't actually post all my code but will try to explain as well as possible and post only small part of code.

    I have 3 actions in one of my controller :

    csharp Code:
    1. [AcceptVerbs(HttpVerbs.Get)]
    2. public ActionResult Manage(int? id, int? periodID)
    3. {
    4.     ....
    5. }
    6.  
    7. [AcceptVerbs(HttpVerbs.Post)]
    8.         public ActionResult Manage(ManageTimeSheetViewData viewData, TimeSheetAction? todo)
    9. {
    10.     ...
    11. }
    12.  
    13. [AcceptVerbs(HttpVerbs.Post)]
    14. public ActionResult SaveDraftAjax(ManageTimeSheetViewData viewData)
    15. {
    16.     ...
    17. }

    Basically, the page is a timesheet, users can enter the time they have worked on each project during the week. What I did is I used jQuery and Ajax to automatically save the timesheet each, let's say 3 minutes, this way that page never timesout and if a user edit his timesheet, and close his browser or tab without clicking on the save button, chances are that his timesheet has been saved anyway.

    Here is the jQuery/Ajax code that does so :

    jQuery Code:
    1. setInterval(function() {
    2.     $('#postForm').ajaxSubmit({
    3.             url: 'SaveDraftAjax',
    4.             dataType: "json",
    5.             success: function(data) {
    6.                 $("[class=SaveDraftMessage]").remove();
    7.                 $.each(data.Messages, function(index, value) {
    8.  
    9.                     $("[id=SaveDraftResult]").append('<li class="SaveDraftMessage">' + value + '</li>');
    10.                 });
    11.             }
    12.         });
    13.     }, 180000);

    The default action of the form "postForm" is "Manage" but I override it in the ajax call by setting url to "SaveDraftAjax".

    Here is the problem: when you enter your timesheet (GET) you can edit values, keep the page open as long as you want and every 3 minutes, the ajax call is called and the "SaveDraftAjax" action is called and everything works fine. at this moment, your address bar shows "http://localhost:60009/TimeSheets/Manage". Now the user decides to click the save button as he wants to make sure everything is saved, which will call the POST action "Manage". this method saves the data, the same way that "SaveDraftAjax" does, but it also sets confirmation messages and then it calls GET "Manage". so the page will be refreshed, showing the confirmation messages, and the address bar now displays "http://localhost:60009/TimeSheets/Manage/94?todo=Save" "94" being my user id in the database and "Save" being the action that was passed to "Manage" action during the POST.

    (OK, I didn't code that and I know that there should have been one action for each "todo" possible values instead of passing that value to a single action, but I simply have no time to refactor the whole thing )

    What happens next, if you stay on that page, is that the Ajax will still be called every 3 minutes, the problem is that now, each time it is called, instead of executing the "SaveDraftAjax" action, the POST "Manage" action is called, which is not what I want, I want "SaveDraftAjax" to be called and executed.

    I simply can't figure out WHY? I have a feeling that it might come from the URL that changed in the address bar. I havent worked much with MVC yet so if anyone has a clue about that...
    Alex
    .NET developer
    "No. Not even in the face of Armageddon. Never compromise." (Walter Kovacs/Rorschach)

    Things to consider before posting.
    Don't forget to rate the posts if they helped and mark thread as resolved when they are.


    .Net Regex Syntax (Scripting) | .Net Regex Language Element | .Net Regex Class | DateTime format | Framework 4.0: what's new
    My fresh new blog : writingthecode, even if I don't post much.

    System: Intel i7 920, Kingston SSDNow V100 64gig, HDD WD Caviar Black 1TB, External WD "My Book" 500GB, XFX Radeon 4890 XT 1GB, 12 GBs Tri-Channel RAM, 1x27" and 1x23" LCDs, Windows 10 x64, ]VS2015, Framework 3.5 and 4.0

  2. #2

    Thread Starter
    Fanatic Member stlaural's Avatar
    Join Date
    Sep 2007
    Location
    Quebec, Canada
    Posts
    683

    Re: ast.net MVC1 - strange behavior when calling actions.

    Well, until I can find the exact problem and a solution to it, I simply decided to call SaveDraftAjax from Manage when the "todo" parameter is null. Its a stupid work around but at least it will work.
    Alex
    .NET developer
    "No. Not even in the face of Armageddon. Never compromise." (Walter Kovacs/Rorschach)

    Things to consider before posting.
    Don't forget to rate the posts if they helped and mark thread as resolved when they are.


    .Net Regex Syntax (Scripting) | .Net Regex Language Element | .Net Regex Class | DateTime format | Framework 4.0: what's new
    My fresh new blog : writingthecode, even if I don't post much.

    System: Intel i7 920, Kingston SSDNow V100 64gig, HDD WD Caviar Black 1TB, External WD "My Book" 500GB, XFX Radeon 4890 XT 1GB, 12 GBs Tri-Channel RAM, 1x27" and 1x23" LCDs, Windows 10 x64, ]VS2015, Framework 3.5 and 4.0

  3. #3
    Hyperactive Member Krokonoster's Avatar
    Join Date
    Jan 2010
    Location
    Cape Town
    Posts
    448

    Re: ast.net MVC1 - strange behavior when calling actions.

    Code:
     url: 'SaveDraftAjax'
    U got a routete defined as such?
    Otherwise you should rather call this with controller/action.

    Do you look at how things get posted using firebug?


  4. #4
    Hyperactive Member Krokonoster's Avatar
    Join Date
    Jan 2010
    Location
    Cape Town
    Posts
    448

    Re: ast.net MVC1 - strange behavior when calling actions.

    Code:
     url: 'SaveDraftAjax'
    U got a routete defined as such?
    Otherwise you should rather call this with controller/action.

    Do you look at how things get posted using firebug?


  5. #5

    Thread Starter
    Fanatic Member stlaural's Avatar
    Join Date
    Sep 2007
    Location
    Quebec, Canada
    Posts
    683

    Re: ast.net MVC1 - strange behavior when calling actions.

    I already tried calling it with the Controller/Action syntax but it didn't work. I'll have a look at defining routes and I will also give a try to firebug. I'm pretty sure the problem comes from the routes, what I don't understand is why it works perfectly well until I click the save button that calls the "Manage" action.
    Last edited by stlaural; Feb 8th, 2011 at 07:58 AM.
    Alex
    .NET developer
    "No. Not even in the face of Armageddon. Never compromise." (Walter Kovacs/Rorschach)

    Things to consider before posting.
    Don't forget to rate the posts if they helped and mark thread as resolved when they are.


    .Net Regex Syntax (Scripting) | .Net Regex Language Element | .Net Regex Class | DateTime format | Framework 4.0: what's new
    My fresh new blog : writingthecode, even if I don't post much.

    System: Intel i7 920, Kingston SSDNow V100 64gig, HDD WD Caviar Black 1TB, External WD "My Book" 500GB, XFX Radeon 4890 XT 1GB, 12 GBs Tri-Channel RAM, 1x27" and 1x23" LCDs, Windows 10 x64, ]VS2015, Framework 3.5 and 4.0

  6. #6
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: ast.net MVC1 - strange behavior when calling actions.

    What does your redirect code in the POST version of Manage look like?

  7. #7

    Thread Starter
    Fanatic Member stlaural's Avatar
    Join Date
    Sep 2007
    Location
    Quebec, Canada
    Posts
    683

    Re: ast.net MVC1 - strange behavior when calling actions.

    There are two possibilities, if the Cancel button was clicked then we redirect the user to a view named "ViewActions"

    Csharp Code:
    1. return RedirectToAction("ViewActions", "Admin");

    Otherwise, error message or confirmation message are set and we return the current view which is named "Manage" and we provide a new viewdata.

    Csharp Code:
    1. return View("Manage", viewData);
    Alex
    .NET developer
    "No. Not even in the face of Armageddon. Never compromise." (Walter Kovacs/Rorschach)

    Things to consider before posting.
    Don't forget to rate the posts if they helped and mark thread as resolved when they are.


    .Net Regex Syntax (Scripting) | .Net Regex Language Element | .Net Regex Class | DateTime format | Framework 4.0: what's new
    My fresh new blog : writingthecode, even if I don't post much.

    System: Intel i7 920, Kingston SSDNow V100 64gig, HDD WD Caviar Black 1TB, External WD "My Book" 500GB, XFX Radeon 4890 XT 1GB, 12 GBs Tri-Channel RAM, 1x27" and 1x23" LCDs, Windows 10 x64, ]VS2015, Framework 3.5 and 4.0

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width