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.

1
  • 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). Commented Nov 3, 2014 at 17:17

1 Answer 1

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>
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. I also liked the insights provided by @TrueBlueAussie in the 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.