Reputation: 123
I'm building an ASP.NET MVC application with a lots of jQuery ajax calls (usually to refresh lists etc.). I'm using $.load mostly. The rebinding/action js scripts are naturally in Scripts folders in .js files. At this moment I'm holding all links in _Layout.cshtml like this
var GET_ARTICLE_LIST = '@Url.Action("List","Article",null)';
var GET_FILES_LIST = '@Url.Action("Files","Library", null)';
then I'm using it in a code like this
$('.element').load(GET_ARTICLE_LIST);
is there any nicer way to pass ASP.NET MVC @Url.Action link to .js files?
Upvotes: 0
Views: 79
Reputation: 15360
You could store the url in a data-attribute of the element that triggers the ajax call (i.e. a button or link etc)
<input type="submit" name="submit" data-url="@Url.Action("Action", "Controller")" />
var url = $(your_element).data('url');
$('.element').load(url);
Upvotes: 2
Reputation: 1135
Have you tried to make a javascript function in the Layout that returns you the url? For example:
function GetUrl(controller, action){
return '@Url.Action("ACTION", "CONTROLLER")'.replace('ACTION',action).replace('CONTROLLER',controller);
}
so you don't have to write all this urls.
Upvotes: -1