1

I am using ajax to fill a series of dropdown list in an MVC 3 razor environment and am trying to create a general function to do this. The core bit of the function I am having problems with is

url: '@Url.Action("SecondaryList")'

where I want to replace "SecondaryList" with a JavaScript variable.

So

var myUrl = 'SecondaryList'
url: '@Url.Action(' + myUrl + ')'

I have no idea how to do it as the above does not work!

Many thanks in advance!

1 Answer 1

3

You could use the .replace() javascript function. Start by declaring a javascript variable

var myUrl = 'SomeAction';

and then use a dummy placeholder with the server side Url.Action helper that you could replace with the javascript variable:

url: '@Url.Action("__url__")'.replace('__url__', myUrl)
Sign up to request clarification or add additional context in comments.

1 Comment

Excellent answer - simple, elegant and 100% effective. (Also, obvious to me once seen). Many thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.