js ->
var favoriteIcons = document.querySelectorAll(".favoriteIcon");
favoriteIcons.forEach((item) => {
item.addEventListener("click", function() {
var itemPPElement = item.parentElement.parentElement;
var id = itemPPElement.id;
var customer_id = itemPPElement.querySelector(".customer_id").value;
var product_id = itemPPElement.querySelector(".product_id").value;
var jsonObject = {
favorite_id: id,
customer_id: customer_id,
product_id: product_id
};
console.log(jsonObject);
fetch('/Favorite/SwitchFavorite', {
method: "POST",
body: JSON.stringify(jsonObject),
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json' }
})
.then(response => response.json())
.then(json => {
item.classList.toggle("la-heart");
item.classList.toggle("la-heart-o");
console.log(json);
})
.catch(err => console.log(err));
});
});
Controller ->
[HttpPost]
public JsonResult SwitchFavorite(Favorite favorite)
{
return Json(favorite);
}
I will capture my json data in the controller and process it, but I return the incoming data directly to control my data, but it does not capture the data. what should I do ?
Try changing
[HttpPost]
public JsonResult SwitchFavorite(Favorite favorite)
{
return Json(favorite);
}
to
[HttpPost]
public JsonResult SwitchFavorite([FromBody]Favorite favorite)
{
return Json(favorite);
}