jquery - Trouble With C# MVC Ajax request -
so having trouble calling function in c# controller. whenever ajax request made, controller automatically redirects main view of controller, "index" method. if put breakpoint, function never reaches function "favorite", , instead redirects. i've been @ while, , don't see i'm doing wrong. thanks.
here ajax function:
var updatefavorites = function (event) { $.ajax({ type: 'post', url: baseurl + '/post/favorite', data: { id: $(this).attr('data-id'), isfavorite: $(this).attr('data-favorite') }, success: function () { }, complete: function () { } }); event.preventdefault();
here favorite method:
[system.web.mvc.httppost] public jsonresult favorite(int id, bool isfavorite) { "code here..." }
here default view:
public actionresult index() { "code here..." }
does have idea happening? i've been staring @ code , trying different things hours can't seem find bug. thanks!
edit:
here things asked for:
routing config:
routes.maproute("specificpost", "post/{title}/{action}", new { controller = "post", action = "details", }, new { title = @".*" }); routes.maproute("create", "post/{action}", new { controller = "post", action = "create", }); // mvc default routes.maproute("default", "{controller}/{action}", new { controller = "post", action = "index" });
html(this button, not sure else want):
<span class ="btn favoritebutton" data-id="@model.id" data-favorite="@model.isfavorite">favorite</span>
also jquery:
$('.favoritebutton').click(updatefavorites);
it looks me following route definition redirect ../post/create route matching "post/{anything}"
routes.maproute("create", "post/{action}", new { controller = "post", action = "create", });
you remove route definition and, long request comes in http 'post', default route:
routes.maproute("default", "{controller}/{action}", new { controller = "post", action = "index" });
will correctly route "post/favorite" postcontroller.favorite() action.
Comments
Post a Comment