Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
32
rated 0 times [  39] [ 7]  / answers: 1 / hits: 98662  / 12 Years ago, mon, january 21, 2013, 12:00:00

How do I render a Boolean to a JavaScript variable in a cshtml file?



Presently this shows a syntax error:



<script type=text/javascript >

var myViewModel = {
isFollowing: @Model.IsFollowing // This is a C# bool
};
</script>

More From » asp.net-mvc

 Answers
49

You may also want to try:



isFollowing: '@(Model.IsFollowing)' === '@true'


and an ever better way is to use:



isFollowing: @Json.Encode(Model.IsFollowing)

[#80722] Sunday, January 20, 2013, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
devinjadong

Total Points: 711
Total Questions: 117
Total Answers: 100

Location: Andorra
Member since Sat, May 27, 2023
1 Year ago
devinjadong questions
Thu, Feb 17, 22, 00:00, 2 Years ago
Wed, Dec 8, 21, 00:00, 2 Years ago
Tue, Oct 27, 20, 00:00, 4 Years ago
Fri, Oct 18, 19, 00:00, 5 Years ago
;