As an example, see this question and its answer:
How to pass a razor variable to jquery function as parameter .
The answer is correct, but there is no clarification on the why and I would like to understand it. If the parameter passed was for example var b = 1
or var b = "xpto" it works, but if it is var b = @object.Name
which returns "xpto" it requires the surrounding'. Is there any explanation for this? Thank you.
1 Answer
This has to do with JavaScript syntax.
If you write:
console.log(foo);
You will get: Uncaught ReferenceError: foo is not defined
If instead you write:
console.log('foo');
It will print foo.
It's important to realize that razor runs on the server so what your browser sees (and feeds into JavaScript) is:
<a href="#" onclick="Edit('foo');">edit</a>
and not
<a href="#" onclick="Edit('@Interest');">edit</a>
1 Comment
PF_learning
Thank you. I also liked the insights provided by @TrueBlueAussie in the comments.
@object.Namedoes not return"xpto"it actually returns justxpto(the quotes are added by the debugger for display - so you know it is a string).