0

The jQuery posting event is

<script>
    $(document).ready(function () {
        $("#going").click(function () {
            $.post("/Home/ToggleGoing", 
             { 
                 going: $("#going").val() 
             }, 
             function (data) {
               //No success code.   
            });
        });
    });
</script>

And the action in control is

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult ToggleGoing(bool going, int? id)
    {
        var e = db.Events.FindAsync(id);

However, the javascript console shows it got 500 error?

POST https://localhost:44300/Home/ToggleGoing 500 (Internal Server Error)
Brian Webb
  • 1,146
  • 1
  • 14
  • 29
ca9163d9
  • 27,283
  • 64
  • 210
  • 413
  • You have not passed a value for ID in the jquery method. Please refer to the answers in your previous question. –  Aug 23 '14 at 06:36
  • If your passing ID as 0, then its probably because `var e = db.Events.FindAsync(0);` throws an exception (and why `int?` instead of `int`)? –  Aug 23 '14 at 06:50
  • first check id is not null then select event from db or make id non-nullable which default value '0' will be set. – Mohsen Esmailpour Aug 23 '14 at 06:51

3 Answers3

4

This: ValidateAntiForgeryToken.

When you use $.post() the hidden field that contains the validation token is not being included. You need to include it in your POST data:

$.post("/Home/ToggleGoing", 
    { 
        going: $("#going").val(),
        __RequestVerificationToken: $('input[name="__RequestVerificationToken"]').val()
    }, function (data) {

});

Of course, this assumes that you've added a @Html.AntiForgeryToken() to your form somewhere.

Tieson T.
  • 20,774
  • 6
  • 77
  • 92
  • I changed the jquery posting to `$.post("/Home/ToggleGoing", { going: $("#going").val(), id: 0, __RequestVerificationToken: $('input[name="__RequestVerificationToken"]').val()}, function (data) {});` but it still get the error. – ca9163d9 Aug 23 '14 at 07:02
  • So what happens when you debug? Is your action being called, or does it fail earlier in the request? – Tieson T. Aug 23 '14 at 07:04
  • Have you installed Elmah or similar? You're just going to be guessing, otherwise. If you have Firebug installed, you can view the HTML from the post result, which will let you see the YSOD with a stacktrace. – Tieson T. Aug 23 '14 at 07:21
1

Correct your Post url :

$.post("/Home/ToggleGoing", { going: $("#going").val() , id:???//pass value of id }, function (data) {})

Make sure going is of type bool.

EDIT :-

as my suggestion in comments section worked for the questioner so i m adding that also in answer.

Just remove [ValidateAntiForgeryToken] token from Post controller or include ValidateAntiForgeryToken as a parameter in $.Post().

Kartikeya Khosla
  • 18,743
  • 8
  • 43
  • 69
  • Good eye for detail! But you should explain *why* this fixes the problem. Stack Overflow exists to teach not just tell. Good luck! :) – jamesmortensen Aug 23 '14 at 06:38
  • I changed the jQuery posting to `$.post("/Home/ToggleGoing", { going: $("#going").val(), id: 0 }, function (data) {` for testing and it still get the 500 error. – ca9163d9 Aug 23 '14 at 06:47
  • just change going in action as string and check...if you are using bool on action make sure its bool sending from $.post..@dc7a9163d9 – Kartikeya Khosla Aug 23 '14 at 06:48
  • I changed the action to `public ActionResult ToggleGoing(string going, int? id)` but still get the error. – ca9163d9 Aug 23 '14 at 06:55
  • just remove ? from int make it int id...and what's there in 'going' is it a bool or string????..and remove [ValidateAntiForgeryToken] from post controller...@dc7a9163d9 – Kartikeya Khosla Aug 23 '14 at 06:56
  • @dc7a9163d9...check controller name – Kartikeya Khosla Aug 23 '14 at 07:04
  • Just did some test. It's OK for id to have type `int?`. Removing ValidateAntiForgeryToken works. Is ValidateAntiForgeryToken necessary? – ca9163d9 Aug 23 '14 at 07:09
  • may be or not...i have never used it ...@dc7a9163d9 – Kartikeya Khosla Aug 23 '14 at 07:12
  • @dc7a9163d9 This summarizes what the AntiForgeryToken is: http://stackoverflow.com/a/13622061/534109 – Tieson T. Aug 23 '14 at 07:27
  • @dc7a9163d9...always try to accept the answer which solves your problem so that it will help other users also..and try to accept the first appropriate answer... not like this http://stackoverflow.com/questions/25458717/why-asp-net-mvc-5-put-scripts-render-bundles-jquery-at-the-bottom-in-layo/25458758#25458758 ....where person answered second got as accepted answer...thanks.. – Kartikeya Khosla Aug 23 '14 at 08:08
0

I have answered the almost similar question at:

How to get {ID} in url “controller/action/id” in a post action?

As discribed in the above answer, you are also missing the id in the end of url as it is mapped like "{controller}/{action}/{id}" in RouteConfig.cs file in RegisterRoutes method. So your corrected Url will be:

"/Home/ToggleGoing/{id}"  //in your case: "/Home/ToggleGoing/4"

Next is "going" is bool parameter. and to find the check box is checked or not "val()" is not the correct method, you should use it as:

$("#going").is(':checked')
Community
  • 1
  • 1
Hassan Baig
  • 346
  • 1
  • 10