1

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.

Community
  • 1
  • 1
PF_learning
  • 25
  • 13
  • 1
    1) As a guide, do not inject values into page-based JS code. Too many problems with that. Inject data attributes into the page instead. 2) The way you are doing it, it will inject the value (without quotes) whereas you need a string literal in the Javascript. `@object.Name` does not return `"xpto"` it actually returns just `xpto` (the quotes are added by the debugger for display - so you know it is a string). – iCollect.it Ltd Nov 03 '14 at 17:17

1 Answers1

2

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>
Halcyon
  • 57,230
  • 10
  • 89
  • 128