0

How do I pass javascript variable into MVC RenderAction parameter?

@{Html.RenderAction("GeneratePlayersSelect", new { teamid = ??? ] });}

Please help! Thanks in advance.

1
  • 1
    You cant. Razor code is parsed on the server before its sent to the client (javascript is client code so its not even in scope) Commented Mar 5, 2015 at 2:55

1 Answer 1

1

May be you can do it. Just have a id assigned to your Action and set href property again in JavaScript, like this:

@{Html.RenderAction("GeneratePlayersSelect", "your_controller", new { id= "lnkPlayer" });}

And in JavaScript (after document loads), change the href:

document.getElementById('lnkPlayer').href = document.getElementById('lnkPlayer').href + '?teamid='+ yourVariable;

Which will change to 'your_controller/GeneratePlayersSelect?teamid=1'

Hope it works, thank you.

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

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.