Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
See more: Javascript MVC Parameter , +
How can I call a javascript function within a view(cshtml) and pass some string variables (defined in the view)to be used as parameters for the function call?
Say the function javascriptFunction uses 2 parameter. I will usually call it as
javascriptFunction('param1', 'param2')
But now I want to pass it some variables.
string y = "this is a string"
string x = "another"
javascriptFunction(y, x)
I have tried
javascriptFunction(@y, @x)
javascriptFunction('@y', '@x')
but these do not work
Posted 8-Jan-13 12:28pm
Member 8044586 at 8-Jan-13 17:29pm
javascriptFunction('@y', '@x') gives me a syntax error during compilation

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You need to pass the values in through a model, then @Model.X and @Model.Y will work. Where do x and y come from ? If they are javascript variables, then remove the @, which tells the compiler to use Razor, when you want local values.
Member 8044586 at 8-Jan-13 17:43pm
x and y are variables derived in my view. I tried creating passing the values to the model and calling @Model.X but got the same error. It is a syntax error.
Christian Graus at 8-Jan-13 17:47pm
Perhaps if you told us what the error is ? And the actual code that creates them and where it lives ?
Member 8044586 at 8-Jan-13 17:51pm
The code is <input type="button" önclick="javascript: function('param1', @Model.temp);" value="Test" />. The error simply says: Syntax error with red markings before the @symbol and after closing the parentheses. So the markings surround @Model.temp)
Christian Graus at 8-Jan-13 18:08pm
You do need quotes. '@Model.temp'. The value will get rendered without quotes. This is a run time error ?
Christian Graus at 8-Jan-13 18:09pm
Also, as you have quotes, param1 will be passed as "param1", not the value in a param named that.
Member 8044586 at 8-Jan-13 20:29pm
when I have it as <input type="button" önclick="javascript: function('param1', '@Model.temp');" value="Test" />, i get the error: Unterminated string constant Expected ')' with with red markings before the @symbol; and the quote before the close of the parentheses generates another error: Unterminated string constant
Christian Graus at 8-Jan-13 21:08pm
Edit your post to show your whole page so we can see what is going on.
Christian Graus at 8-Jan-13 21:08pm
I would also suggest trying to make your click event call a function that you define further down on the page

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 300
1 ProgramFOX 165
2 Peter Leow 130
3 Zoltán Zörgő 110
4 Maciej Los 80
0 OriginalGriff 260
1 ProgramFOX 145
2 Peter Leow 130
3 Sergey Alexandrovich Kryukov 85
4 Fredrik Bornander 75

Advertise | Privacy | Mobile
Web04 | 2.8.150301.1 | Last Updated 17 Jan 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100