0

I have a ASP.NET MVC application and I have a few entries on the page that user can change and click Save and I go save those entries. My problem: It works fine for some entries and for other entries it just doesn't go in the controller Save function to do the save. My code:

 function DoSave() {
         $("#pisave").attr("disabled", true);
         var pid = $("#personid").val();alert(pid);
         var firstname = $("#fname").val();alert(firstname);
         var lastname = $("#lastname").val();alert(lastname);
         var plz = $("#zip").val();alert(plz);
         var ort = $("#city").val();alert(ort);
         var bday = $("#birthdate").val();alert(bday);
         var strasse = $("#street1").val(); alert(strasse);
         var emailtext = $("#email").val();alert(emailtext);
         var url = "@(Url.Action("SavePersonInfo", "Info"))";alert("URL");
        $.ajax({
            url: url,
            data: { personid: pid,fn: firstname, ln: lastname, email: emailtext, zip: plz, city:ort, birthday: bday, street:strasse },
            success: function () {                    
            alert("Update Successful");
            $("#pisave").removeAttr("disabled");
            },
           error: function () {                    
              alert("Update Failed! Check entries.");
             $("#pisave").removeAttr("disabled");
            }
        });
    }  

All alerts are displayed in all the cases. Only for some it goes to SavePersonInfo and for others it doesn't go in there. Any ideas what might be wrong?? Can it be validation issue for the entries?

Tulips
  • 869
  • 6
  • 12
  • 22
  • How the controller looks like? does it fail? what are the values when it doesn't work? and the title you choose is the worse I saw for a long time! – gdoron Jun 27 '12 at 09:00
  • The controller works okay and I can update some entries without any problem. Only in 'some' cases it does not even go to the controller as I can see when I put a break point. – Tulips Jun 27 '12 at 09:16
  • Sorry about the title..I know its not informative and may be I should change it to javascript error in mvc application? – Tulips Jun 27 '12 at 09:17
  • Answer **ALL** the question in my comment. and maybe it a cache thing? – gdoron Jun 27 '12 at 09:20
  • How/where are you calling this `DoSave` javascript function? – Darin Dimitrov Jun 27 '12 at 09:23
  • @gordon: Values look just the same to me for the cases it works and cases it doesn't work for. I am looking deeper into the values to find some pattern. I will update shortly. – Tulips Jun 27 '12 at 09:33
  • @DarinDimitrov I call it on the web page like this:
    – Tulips Jun 27 '12 at 09:34
  • do you get a javascript error for the cases which does not work? is the ajax even fired for that cases? ......Also have you checked data for `personid: pid,fn: firstname, ln: lastname, email: emailtext, zip: plz, city:ort, birthday: bday, street:strasse` for cases which does not work? – Kshitij Jun 27 '12 at 09:40
  • @Kshitij I think its fired because I get the Update not successful message from Ajax. I think the pattern is with the birthdate format. if its mm:dd:yyyy it works fine and for cases dd:mm:yyyyy it doesnot work ! Now, what is the fix for this? – Tulips Jun 27 '12 at 09:46
  • Was your problem solved? – gdoron Jun 27 '12 at 10:33
  • I am going to try. We had a long meeting so I had to go. Thanks. – Tulips Jun 27 '12 at 12:04

1 Answers1

0

The model binder fails to parse your date, change to post:

$.ajax({
    type: "POST",
    url: url,
    data: { personid: pid,fn: firstname, ln: lastname, email: emailtext, zip: plz, city:ort, birthday: bday, street:strasse },
    success: function() {
        alert("Update Successful");
        $("#pisave").removeAttr("disabled");
    },
    error: function() {
        alert("Update Failed! Check entries.");
        $("#pisave").removeAttr("disabled");
    }
});​

Read more about the problems with dates in asp.net-MVC

Note that you can add all the elements a class and use the serialize function:

$.ajax({
    type: "POST",
    url: url,
    data: $('.theClass').serialize(), // <=============
    success: function() {
        alert("Update Successful");
        $("#pisave").removeAttr("disabled");
    },
    error: function() {
        alert("Update Failed! Check entries.");
        $("#pisave").removeAttr("disabled");
    }
});​
Community
  • 1
  • 1
gdoron
  • 147,333
  • 58
  • 291
  • 367
  • I do get the message "Update Failed! Check entries." but I never reach the breakpoint in my controller. Let me try the Cache thing. – Tulips Jun 27 '12 at 09:35
  • @Tulips. If you get the error, this is not about the cache. Show the controller Action signature looks like and what are the values causing the failure. – gdoron Jun 27 '12 at 09:37
  • Yeah, its not about the Cache. This is my controller: public JsonResult SavePersonInfo(Guid personid, string fn, string ln, string email, int zip, string city, DateTime birthday, string street) { } – Tulips Jun 27 '12 at 09:40
  • I think the pattern is with the birthdate format. if its mm:dd:yyyy it works fine and for cases dd:mm:yyyyy it doesnot work ! Now, what is the fix for this? – Tulips Jun 27 '12 at 09:42
  • @Tulips. [Read my answer here](http://stackoverflow.com/a/8035636/601179). you can solve it with changing from get to POST: `$.ajax({ type: 'POST', url: url, ...}) ` – gdoron Jun 27 '12 at 09:45
  • @Tulips. If that was it accept the answer, if not and you will have diffculties tell us what they are. If you had other solution write it and accept that answer. Good luck. – gdoron Jun 27 '12 at 12:19
  • What I did was to change the DateTime to String in the controller function and then Convert it to DateTime again. DateTime birthday1 = Convert.ToDateTime(birthday); – Tulips Jun 27 '12 at 12:53