I am currently working on an ASP.NET MVC5 web application and have a controller action post method that resembles the following code snippet:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Set(int id)
{
DbHelper dbHelper = new DbHelper();
dbHelper.Update<News>("update news set ispublished = 1, publishdate = @PublishDate where newsid = @NewsId", new { NewsId = id, PublishDate = DateTime.Now });
return Json(new { success = true, message = "added successfully!", redirectToUrl = Url.Action("NewsList", "Home") }, JsonRequestBehavior.AllowGet);
}
When trying to call this method from my View using Ajax:
$(document).on("click", ".set", function () {
var mId = $(this).attr("data-model-id");
$.ajax({
url: "@Url.Action("Set","Home")",
data: { "id": mId },
contentType: 'application/json',
type: 'POST',
dataType:'json',
success: function (response) {
if (response.success) {
window.location.href = response.redirectToUrl;
dt.ajax.reload();
$.notify(data.message, {
globalPosition: "top center",
className: "success"
});
}
},
error: function (response) {
$.notify(response.message, {
globalPosition: "top center",
className: "success"
});
}
});
});
However, I keep encountering the issue shown in this image.
My expected outcome is to be redirected to the home/newslist URL. Despite multiple attempts at modifying the parameters of the Ajax call, the result remains unchanged.