6

How can I put javascript code inside if block.

@{
    #if DEBUG
    $("#User").val("JDoe");
    $("#Password").val("secrect");
    #endif
}

When I try above code I get this compiler error:

Compiler Error Message: CS1056: Unexpected character '$'

And if I change $ to jQuery:

Compiler Error Message: CS0103: The name 'jQuery' does not exist in the current context

In my opinion it's a bug in Razor parser. How can I workaround that?

2 Answers 2

7
@{#if DEBUG}
    $("#User").val("JDoe");
    $("#Password").val("secrect");
@{#endif}
Sign up to request clarification or add additional context in comments.

Comments

1

Wrap the javascript code within tag.

@{(if DEBUG)
    **<text>**
    $("#User").val("JDoe");
    $("#Password").val("secrect");
   **</text>**
}

1 Comment

This is a better answer, since <text> pseudo tag works in more cases.Eg., it could work with for loops too.

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.