I am trying to make some mvc application and I have a problem. I want to get json from mvc controller in javascript file:
JS:
var actionUrl = '@Url.Action("GetJsonTest", "JsonTest")';
$.getJSON(actionUrl, function (response) {
if (response != null) {
var html = "<li>" + response.Id + " " + response.Name + " " + response.Author + "</li>";
$("h2").append(html);
}
});
Controller:
[HttpGet]
public ActionResult GetJsonTest() {
var books = new Book() {
Id = '1',
Name = "1+1",
Author = "Varnava",
Price = 100
};
return Json(books, JsonRequestBehavior.AllowGet);
}
It's not working. I have 404 error. It cant get something from controller. What am I doing wrong? Sorry for bad English. Hope you understand what I ask.
GetJsonTestmethod is insideJsonTestController.cscshtmlfiles will be intepreted by the MVC template server. Anything else will not so your js file is tryin to make the literal request to url'@Url.Action("GetJsonTest", "JsonTest")'. If you did some debugging in your browser this would be immediately obvious.