Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6988383
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:04:28+00:00 2026-05-27T19:04:28+00:00

I am using ASP.Net MVC 3 Razor view engine. I have a requirement to

  • 0

I am using ASP.Net MVC 3 Razor view engine.

I have a requirement to generate some JavaScript code in my View based on a value in my View Model. The value I need to use is a boolean, for this example lets call it IsSet.

So what I want to do is create a JavaScript boolean based on this value that I can use in the script later on.

Keep in mind that for all below examples I have this bit of code at the top of my view…

@{ string IsSet = Model.IsSet  ? "true" : "false"; }

NOTE: All examples below are JavaScript.

First attempt…

var IsSet = @(IsSet);

… this actually works, the problem is it breaks the auto-formatting (CTRL + E, D) in VS 2010 due to badly formatted JavaScript – as you might expect, and this is not acceptable.

Second attempt…

var IsSet = "@(IsSet)";

…I know, JavaScript is clever, it will auto-parse my string when needed. Ooops, forgot it is a string type and anything other than empty evaluates to true.

Third attempt…

var IsSet = Boolean("@(IsSet)");

….surely this will work… nope, convert non-empty string to true again (bad parser!)

Fourth attempt…

var IsSet = "@(IsSet)" === "true";

Finally something that works, but it doesn’t look great to me.

I will use this if need be but ultimately my question is: Is there a better way to handle this kind of situation? Perhaps, the unwanted behaviour in my first attempt is just something that Microsoft may have overlooked?

If anybody has a nice and tidy fifth attempt for me, that would be good.

The important thing for me is that the auto-formatting in VS 2010 does not break

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T19:04:29+00:00Added an answer on May 27, 2026 at 7:04 pm

    Version 1 is the only one of those that I’d vote for even if they all worked, because it’s the most human-readable. Unfortunately I don’t have VS at home so I can’t try it out to see what the auto-formatting issue is, but if at all possible I’d want to ignore the issue and go ahead and use that version given that there’s nothing actually wrong with the code – it is just VS that is confused. (Are you saying VS is trying to interpret the whole thing as JS and thus finding it invalid?)

    But if you want some other options:

    Fifth attempt…

    @{ string IsSet = Model.IsSet  ? "true" : ""; }
    
    var isSet = !!"@(IsSet)";
    // OR
    var isSet = Boolean("@(IsSet)");
    

    Coerce the string value into a boolean with the old double-not-operator trick – as you already pointed out both “true” and “false” would become true, but this problem goes away if you use “true” and “” (empty string) – so you can use Boolean() as per your “third attempt”.

    Sixth attempt…

    @{ string IsSet = Model.IsSet  ? "true" : "false"; }
    
    // helper function at the top of your page:
    function bool(strVal) {
       return strVal === "true";
    }
    
    // variable declaration
    var isSet = bool("@(IsSet)");
    

    OK, so you end up with a fairly pointless function at the top of your page, but it keeps the actual variable declarations reasonably tidy and if debugging client side you’ll see bool("false") or bool("true").

    Seventh attempt…

    I don’t like the extra step of creating the server-side string IsSet = Model.IsSet ? "true" : "false"; in advance. I don’t know Razor syntax, but can you say something along the lines of:

    var isSet = !!"@(Model.IsSet ? "true" : "")";
    // OR, better:
    var isSet = !!"@(rzrBool(Model.IsSet))";
    // where rzrBool() is a server-side helper function (that you would create)
    // which returns "true" or ""
    

    I would expect all of my “attempts” to work, but again I think your “first attempt” is the best option.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using ASP.NET MVC 3 with the Razor view engine . I have
I am using ASP.NET MVC 3 with the razor view engine. I have the
I'm experimenting with an ASP.NET MVC 3 site, using razor as the view-engine. I
I am using Razor view engine in ASP.Net MVC 3 RC 2. This is
I am programming using ASP.NET MVC 3 with Razor view engine. Here's what I'd
I am using jQuery and ASP.NET MVC 3 with the razor view engine .
I am using jQuery and ASP.NET MVC 3 with the razor view engine .
I am using ASP.NET MVC 3, with the Razor engine. I have a partial
Using the new ASP.NET MVC 3.0 Razor View Engine, is there any way to
Using ASP.NET MVC can be used the view engine Razor. Razor let you use

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.