Reputation: 571
Hi I have a litle jquery script
$(document).ready(function () {
$.ajax({
url: '@Url.Action("AjaxTest", "Home")',
})
});
When I include it directly in the layout page of my MVC application it works as I expect. When I'm trying to call it from an external file(infoAjax.js) it fails and I get message: Failed to load resource: the server responded with a status of 404 (Not Found). My bundle file:
bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include(
"~/Scripts/bootstrap.js",
"~/Scripts/respond.js",
"~/Scripts/infoAjax.js"
));
and responsible part of layout page:
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")
@RenderSection("scripts", required: false)
Upvotes: 1
Views: 873
Reputation: 583
As .js files are not parsed by asp.net mvc view engine, you simply cannot use any code in there. I would suggest using unobtrusive approach, something like this
<div id="loader" data-request-url="@Url.Action("Action", "Controller")"></div>
And in javascript, use value of data-request-url
$(function(){
$('#loader').click(function(){
var url = $(this).data('request-url');
alert(url);
});
});
Upvotes: 3
Reputation: 6944
@Url.Action("AjaxTest", "Home") is avaible only View of MVC project. Try to use exact address like
url: 'localhost/Home/AjaxTest',
instead of
url: '@Url.Action("AjaxTest", "Home")',
Upvotes: 2