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.