Reputation: 571
i just created a Javascript function and a controller method based on another one that is working as expected. Javascript value seems to be right, but when it reachs the controller, it is changed to 0. I tried re-doing the entire function from scratch with the same results.
Edit: on console log, e has the right value.
Edit 2: this is how it looks the request. the number is OK! and it is the same way the other method gets the number.
This is the javascript code, where e is the correct value (it is the post ID):
function toggleArchivar(e) { //i've debugged it and e has the correct value
$.ajax({
type: "POST",
url: "/submissions/togglearchivar/" + e,
success: function (t) {
t.success ? (t.data ? $("#submissionid-" + e + " p.title").prepend('<span title="archivado" class="flair
linkflairlabel" id="archivadoTag">archivado</span>') :
$("#archivadoTag").remove(), $("#toggleArchivar").html("nsfw")) :
$("#toggleArchivar").html(t.error.message)
},
error: function () {
alert("Error al intentar archivar.")
}
}) }
And this is the controller method:
[Authorize]
[HttpPost]
[VoatValidateAntiForgeryToken]
[PreventSpam(5)]
public async Task<ActionResult> ToggleArchivar(int submissionID) //SubmissionID comes with a 0!!!
{
using (var repo = new Repository(User))
{
var response = await repo.ToggleArchivar(submissionID);
return JsonResult(response);
}
}
Take for example the working ones, which i debugged and the value matches: Javascript:
function toggleNSFW(e) {
$.ajax({
type: "POST",
url: "/submissions/togglensfw/" + e,
success: function (t) {
t.success ? (t.data ? $("#submissionid-" + e + " p.title").prepend('<span title="Not Safe For Work" class="flair
linkflairlabel" id="nsfwflair">NSFW</span>') :
$("#nsfwflair").remove(), $("#togglensfw").html("nsfw")) :
$("#togglensfw").html(t.error.message)
},
error: function () {
alert("Error al intentar poner NSFW.")
}
}) }
Controller:
[Authorize]
[HttpPost]
[VoatValidateAntiForgeryToken]
[PreventSpam(5)]
public async Task<ActionResult> ToggleNSFW(int submissionID)
{
using (var repo = new Repository(User))
{
var response = await repo.ToggleNSFW(submissionID);
return JsonResult(response);
}
}
As you can see, my functions are a copy of the previous ones. I can't tell why is it coming with that value!
Upvotes: 1
Views: 214
Reputation: 3246
The default value for a int
type variable is 0. As a result since the submissionID
is not getting bound the variable is having the default value.
Check the RouteConfig
(usually available within App_Start
folder). There might be routes written for the ToggleNSFW
action but not for your action - ToggleArchivar
.
Add the following route and check if it works:
routes.MapRoute(
name: "ToggleArchivar",
url: "{controller}/{action}/{submissionID}",
defaults: new { controller ="YourControllerName", action = "ToggleArchivar", submissionID = UrlParameter.Optional }
);
Replace "YourControllerName" name with the actual name of the controller.
Another option is to rename the submissionID
parameter to the id
so that default model binder would bind the value.
Upvotes: 3