-1

I am trying to send a string to my ActionResult in the controller. I have followed many tutorials and read hundreds of stackoverflows but can't get it to work. I am trying to send a string with the value of a radiobutton.

My ActionResult code is:

[HttpPost]
    public ActionResult Opslaan(string theStatus)
    {
        if(theStatus!= null)
            Database.UpdateAanvraagStatusByGuid(Session["Guid"].ToString(), theStatus);
        return new RedirectResult(Request.UrlReferrer.OriginalString);
    }

My code to send the variable via AJAX:

$("#opslaan").click(function (e) {
    e.preventDefault();

    var theStatus = $('input[name=StatusOptions]:checked').val();

    $.ajax({
        type: "POST",
        url: "/Aanvraag/Opslaan",
        data: theStatus,
        success: function (result) {
            if (result.Success) {
                alert("Uw wijzigingen zijn opgeslagen.");
            } else {
                alert(result.Message);
            }
        }
    });
});

When I click my button called "opslaan" the program does not execute te AJAX. Alerts around it do go off.

Thanks in advance :)

  • Have you checked the console or network tab of your browser tools to get more info about the problem? – Brad C Jun 26 '15 at 12:59
  • @br4d the network tab does say it is sending something but it 404's – Luc Schuttel Jun 26 '15 at 13:01
  • 1
    Use `data: { theStatus: theStatus },` –  Jun 26 '15 at 13:03
  • The Post is being sent but still 404's. Same as without this modification. – Luc Schuttel Jun 26 '15 at 13:08
  • You need to make the change as per my comment anyway, but a 404 means that your url ("/Aanvraag/Opslaan") does not exist - check it! In any case you should never hard code url's - use `url: '@Url.Action("Opslaan", "Aanvraag")',` –  Jun 26 '15 at 13:19
  • I was using the .Action method before, but since it wouldnt work i tried this. Changing it back still doesnt work. – Luc Schuttel Jun 26 '15 at 13:24
  • Then manually type that address in your browser - does it still throw the same 404? Who knows - perhaps your using areas and you have not specified the area –  Jun 26 '15 at 13:31

2 Answers2

2

Edit Fabio's answer like this:

$("#opslaan").click(function (e) {
e.preventDefault();

var theStatus = $('input[name=StatusOptions]:checked').val();

$.ajax({
    type: "POST",
    url: "/Aanvraag/Opslaan?theStatus= " + theStatus ,
    //data: { 'theStatus': theStatus } ,
    success: function (result) {
        if (result.Success) {
          alert("Uw wijzigingen zijn opgeslagen.");
        } else {
          alert(result.Message);
        }
    }
    });
});

Note the query string at the end of the url property. Even though string IS a nullable type, if you don't have any route configuration like "/Aanvraag/Opslaan/theStatus", the routing system will not find a match.

There are a few things to note here:

  • Your original solution DID show an alert, that means a request went to the server, and a response has arrived.
  • Fabio's answer didn't work because you (as I guess) don't have any route like "/Aanvraag/Opslaan/theStatus". Even though string is a nullable type - so the routing system will allow a string parameter to have no incoming value from the request - the url parameter set by the client told the routing system 'Hey please forward me to something that is configured to a url like "/Aanvraag/Opslaan/theStatus"'. I am sure You don't have any route set up with that pattern so the routing system will fail to find a matching Controller/Action method pair, that results in a 404.
  • Your original solution didn't cause this problem, because you sent the theStatus parameter as data, and your url was "/Aanvraag/Opslaan". This means even the default route will be able to find out that the Controller is 'Aanvraag' and the controller is 'Osplaan'. From then on, Model Binding was able to bind your theStatus parameter to the action method parameter. (If it wasn't, the proper action method would strill be called, just with a null value given to the parameter.) However, your response didn't send any object with property Success back, so your if statement went to the else branch.

All in all, you can either send the theStatus parameter as data and let the model binding system to bind it to your action method parameter, or use routing to do that for you. In this latter case, you must either configure a proper routing entry or use a query string like in my modified version.

For the if statement to work, you need to send back something that does have a Success property, like Fabio did.

Balázs
  • 2,929
  • 2
  • 19
  • 34
0

It might be helpful:

[HttpPost]
    public ActionResult Opslaan(string id)
    {
        if(id != null)
            Database.UpdateAanvraagStatusByGuid(Session["Guid"].ToString(), id);
        // do your logic to check if success is true or false
        return Json(new { Success = true, Message = "something" });
    }

Javascript:

$("#opslaan").click(function (e) {
   e.preventDefault();

    var theStatus = $('input[name=StatusOptions]:checked').val();

$.ajax({
    type: "POST",
    url: "/Aanvraag/Opslaan/ " + theStatus ,
    //data: { 'theStatus': theStatus } ,
    success: function (result) {
        if (result.Success) {
            alert("Uw wijzigingen zijn opgeslagen.");
        } else {
            alert(result.Message);
        }
    }
});
});

EDIT

Just to see if it works, change the name of the parameter in the Action and Javascript.

Greg B
  • 14,597
  • 18
  • 87
  • 141
Fabio
  • 11,892
  • 1
  • 25
  • 41