Make an AJAX request using $.ajax in MVC 4
You just need to make it an ActionResult
. Also, if you're using an Ajax POST, then the action needs to be marked with the HttpPost
attribute. Try this:
[HttpPost]public ActionResult test(string dealerID){ return Content("It works");}
Edit Actually, there are a few other problems with the syntax.
Url.Action
has the controller/action parameters in the wrong order -- should be "ActionName" first, then "ControllerName"- For
Url.Action
, if the controller class is "HomeController", then you need just "Home" - The JQuery options syntax is wrong -- should be
success: function(data) {}
.
$.ajax({ url: '@Url.Action("test", "Home")', data: {dealerID: dealerID}, type: 'POST', success: function(data) { alert(data); }});