Reputation: 43
I have a ajax call that needs to hit my MVC Action Result. I am getting the success method to fire. It shows the ID that I'm wanting. When I debug my breakpoint is never hit, nor does the method logic fire (redirect to Google in this case).
Basically, I have a list of songs and when the play icon is clicked, I want the Id passed to my controller through ajax so I can count it and save the play count to my database.
Here is my script:
$(document).ready(function() {
$('.playbtn').click(function () {
var clickedSongId = $(this).find('i').data('songid');
var fightsong = { Id: clickedSongId };
$.ajax({
type: "POST",
url: '@Url.Action("UpdatePlayCount", "Team")',
contentType: "application/json; charset=utf-8",
//data: fightsong,
data: fightsong,
dataType: "json",
success: function () { alert('Success'); },
error: function (data) {
alert(clickedSongId);}
});
});
});
Here is my Controller:
[HttpPost]
public ActionResult UpdatePlayCount(int id)
{
return Redirect("http:www.google.com");
}
Upvotes: 2
Views: 1551
Reputation: 1
var url = '@Url.Action("UpdatePlayCount", "Team")';
var clickedSongId = $(this).find('i').data('songid');
var fightsong = { Id: clickedSongId };
jQuery.ajax(url, {type: 'POST', data: fightsong}).done(function(data)
{
//write code with javascript if you want redirect
}).fail(function(jqXHR, textStatus)
{
//write code to handle error
});
Upvotes: 0
Reputation: 29186
You likely need to stringify the data
data: JSON.stringify(fightsong)
Upvotes: 3