1

Im trying to either set the value of a variable to @Model.IsPublic which is a boolean value. In my javascript/jquery i have this.

 var [email protected]

when i put a semicolon to finish the line it gives me an error. Might be a dumb question. but how can i set a variable to the models value

5
  • What? Are you talking about some Microsoft technology? Rails? Commented Jul 23, 2013 at 14:00
  • microsoft c# mvc jquery Commented Jul 23, 2013 at 14:01
  • 1
    Then you should put the correct tags. I see that @tereško fixed it for you. Commented Jul 23, 2013 at 14:02
  • and possibly Razor. In any case retagging required Commented Jul 23, 2013 at 14:02
  • @tereško I'm an ASP MVC user and I completely agree with you :) Commented Jul 23, 2013 at 14:05

2 Answers 2

2

The problem is because when converting bool to string ASP MVC uses uppercase. Javascript expects lowercase. What you are effectively doing right now is this:

var pub = True; // = error (unless you have an entity called 'True' - which I hope you don't)

What you need to do is this:

var pub = @Model.IsPublic.ToString().ToLower() // = var pub = true|false;

Alternatively, if you want to create the javascript variable as a string, you simply need to wrap the Model value in quotes:

var pub = "@Model.IsPublic";
Sign up to request clarification or add additional context in comments.

Comments

1

Try using like this

<script type="text/javascript">
 var pub='@Model.IsPublic';
</script>

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.