Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
163
rated 0 times [  165] [ 2]  / answers: 1 / hits: 18890  / 12 Years ago, thu, march 29, 2012, 12:00:00

I ask a similar question here and Darin Dimitrov answer that we can't use Url helper like $.ajax({ url: '@Url.Action(Index)', . . . in separate js file so what is your suggestion to use Url helper in view page and pass it to javascript, I don't want to use hard code url, I need to find it with Url helper.?


More From » asp.net-mvc

 Answers
23

Use a hidden field to store your url, then use javascript to read the hidden field, then use that in your code. That way you can keep the JS file separate to the view. Something like this:



//In Your View
@Html.Hidden(MyURL, Url.Action(Index))

//In Your JS
var myUrl = $(#MyURL).val();

$.ajax({ url: myUrl , . . .

[#86539] Wednesday, March 28, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
lelasamiraa

Total Points: 208
Total Questions: 99
Total Answers: 107

Location: Uzbekistan
Member since Tue, Nov 30, 2021
3 Years ago
lelasamiraa questions
Thu, Jul 16, 20, 00:00, 4 Years ago
Thu, Oct 17, 19, 00:00, 5 Years ago
Mon, Aug 5, 19, 00:00, 5 Years ago
Thu, Feb 7, 19, 00:00, 5 Years ago
;