I am trying to make an AJAX request using $.ajax in MVC 4 with Razor. I'm not sure how to implement it.
Using this video I was able to successfully make a link-driven call that returned data, but I can't seem to do the same thing from inside a jquery function. I can't seem to find any basic examples of how to do this. This is what I am working with:
HomeController.cs
public string test(){
return "It works";
}
View.cshtml
function inventory(dealerID) {
$.ajax({
url: '@Url.Action("HomeController","test")',
data: {dealerID: dealerID},
type: 'POST',
success: function(data) {
process(data);
}
});
}
You just need to make it an
ActionResult
. Also, if you're using an Ajax POST, then the action needs to be marked with theHttpPost
attribute. Try this: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"Url.Action
, if the controller class is "HomeController", then you need just "Home"success: function(data) {}
.