I am new to MVC coming frm webforms and I am trying to get my head around AJAX.
I want to click a button in my partial view, call a stored proc which updates a count, and then returns the current total count which is returned from the stored proc.
My Jquery looks like this.
function setLikeStatus(id) {
var param = {
dayId: id
};
$.ajax({
url: "/YourDay/LiveHeartClicked",
contentType: "application/x-www-form-urlencoded",
type: "POST",
datatype: "json",
data: param,
error: function (xmlHttpRequest, errorText, thrownError) {
alert(xmlHttpRequest + "," + errorText + "," + thrownError);
},
success: function (data) {
alert(data);
}
});
}
My method looks like this in my controller, I wasn't sure if I sould be using an action result or a JsonResult here. I have tired both but failed.
[HttpPost]
[ValidateAntiForgeryToken]
[Authorize]
public JsonResult LiveHeartClicked(int dayId)
{
YourDayEntities3 context = new YourDayEntities3();
string userId = getUserId();
var countLikes = context.CreateDayLike(dayId, userId);
return Json(countLikes);
}
When I click the button I am just getting the response '[object Object]|error|Internal Server Error'.
Any help would be apprieciated.