×

New Test Blog After Marge The Code

New Test Blog After Marge The Code

Yes, there are a few mismatching pieces between your JavaScript fetch request and your C# Controller method that are keeping them from connecting.

Here are the main issues causing the breakdown, ranked from most critical to least:

1. The Anti-Forgery Token Header Mismatch (Most Critical)

In your JavaScript, you are passing the token using the header key 'RequestVerificationToken':

JavaScript


headers: { ..., 'RequestVerificationToken': getAntiForgeryToken() }

However, standard ASP.NET MVC’s [ValidateAntiForgeryToken] attribute looks for a header named __RequestVerificationToken (with two underscores), or it expects it inside the request body. Because the names don't match, your server is likely rejecting the request with a 403 Forbidden or 500 error before it even hits your controller action.

2. Missing Parameters in the Controller

Your C# controller is expecting a parameter called prmBlogId from the URL route, but it isn't expecting the status variable you are sending in the body.

  1. Your fetch body sends: 'status=' + status
  2. Your controller signature only accepts: int prmBlogId

If you actually want to use that status boolean inside your controller, your C# method needs to accept it.

How to Fix It

Here is the corrected code for both sides to make sure they talk to each other flawlessly.

Updated JavaScript

JavaScript


document.getElementById('blogTable').addEventListener('click', function (e) {
var btn = e.target.closest('.btn-toggle-status');
if (!btn) return;
var id = btn.getAttribute('data-id');
var status = btn.getAttribute('data-status') === '1';
// Ensure you are fetching the token correctly
var token = typeof getAntiForgeryToken === 'function' ? getAntiForgeryToken() : $('input[name="__RequestVerificationToken"]').val();

fetch('/Blog/ToggleStatus/' + id, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
// FIX: ASP.NET looks for '__RequestVerificationToken' by default
'__RequestVerificationToken': token
},
// Sending status in the body
body: 'prmStatus=' + status
})
.then(function (r) { return r.json(); })
.then(function (json) {
if (json.success) window.blogTableManager.reload();
else alert(json.message);
});
});


Get in Touch

We’d love to hear from you. Please fill out this form.

First Name *
Last Name *
Email Address *
Phone Number *
Your FeedBack *

Create: Quick RFQ