0

How to assign JavaScript value under MVC3 Razor?

I am not sure if it will work...

Thank you!

<script type="text/javascript">

      var populate = false; // This is JS variable

       @if (Session["TechList"] == null)
       {
            populate = true;
       }

</script>
1
  • @MarceloAssis Yes It is the Razor. Commented Aug 15, 2012 at 22:10

2 Answers 2

7
var populate =false;
@if (Session["TechList"] == null)
{
    @:populate = true;
}

This will also work

@if (Session["TechList"] == null)
{
  <text>populate = true;</text>
}

But i like to use the first one as it is more clean

Sign up to request clarification or add additional context in comments.

1 Comment

Didn't know this syntax either. Thanks for sharing.
0

My suggestion:

<script type="text/javascript">
     var populate = @(Session["TechList"] == null ? "false": "true");
</script>

1 Comment

Remove 'var' if populate has defined in another place.

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.