31

Is it possible?

@Html.ActionLink( "Link text", "Action Name", new { id = *JAVASCRIPT VARIABLE* })

Please explain with example code.

2
  • No, it's not possible. What exactly are you trying to do? Commented Jul 2, 2012 at 3:30
  • 2
    Actually I need pass a parameter which i get from javascript. How can i solve my problem. Commented Jul 2, 2012 at 3:34

2 Answers 2

77

It actually is very much possible with the following:

var hello = "Great Job!";

var text = "@Html.ActionLink( "Link text", "Action Name", new { id = "JSVar" })"
text = text.replace("JSVar",hello);

As easy as that! :)

All the @Html.Action link does is create a url string so doing replace on it is the same as replace on any javascript string

Sign up to request clarification or add additional context in comments.

3 Comments

Good workaround! Just make sure that the replacement code is robust, maybe adopt a string.Format type approach: var text = "@Html.ActionLink( "Link text", "Action Name", new { id = "{0}" })"; not sure if the {0} will work in that helper, but you get my drift
I'd accept this answer instead of the one accepted now.
n.b. "{0}" is not a good replace target here since the brackets get URLencoded
15

No it is not possible, because .NET MVC 3 is server side code and is evaluated before being sent to the client, and javascript is Client side code which runs only once it is ON the client

BUT the reverse is possible:

var jsVar = "@yourMvcVar";

Comments

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.