First of all slash before the controller name in the url is missing:
"/Home/Toggle"
Next mistake is checkbox value can not be check by using val() method but it can be checked using:
$("#checkbox").is(':checked')
Next you have to append id after the url as it mapped like "controller/action/{id}" in RouteConfig.cs file in RegisterRoutes method.
So your final correct url will be:
"/Home/Toggle/{id}" //in your case: "/Home/Toggle/4"
In Action method "id" parameter is also missing. Corrected Action method is:
[HttpPost]
[ValidateAntiForgeryToken]
public void Toggle(bool checkbox, int id)
{
}
And your complete and corrected Jquery code is:
$.post("/Home/Toggle/4", { checkbox: $("#checkbox").is(':checked') }, function (data) {
});
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…