16

The goal

Return true instead of True from Controller to View.

The problem

I'm storing into a variable a boolean that indicates whether a product exists or not in a shopping cart/summary.

To achieve this, I'm doing the following:

[...]
IsAdded = sessionStore.CheckExistanceOnSummary(product.productId)
[...]

But, when I show the value of IsAdded on the View, the return is True or False — and JavaScript is expecting true or false.

What I need is to send true or false instead of this way that C# is sending.

What I've already tried

I already tried to change the above's code fragment into this:

IsAdded = (sessionStore.CheckExistanceOnSummary(product.productId) ? 
    "true" : 
    "false")

But debugger returns me the following error:

Error 5 Cannot implicitly convert type 'string' to 'bool'

A few lines of code

The implementation of CheckExistanteOnSummary is:

public bool CheckExistanceOnSummary(Nullable<int> productId)
{
    List<Products> productsList = 
        (List<Products>)Session[summarySessionIndex];

    if (productsList.Where
        (product => product.id == (int)productId).FirstOrDefault() == null)
        return false;
    else
        return true;
}

Duplicated?

I read this topic, but didn't not help me.

4
  • Make IsAdded a string then... Jul 17, 2013 at 18:14
  • +1 just for the complete explain question! Use the @Matt Houser solution, I also have this problem with razor view engine and javascript. Jul 17, 2013 at 18:16
  • 3
    In javascript, "1" == true (don't use triple equals) so you could just return a string "1" or "0" instead. Jul 17, 2013 at 18:17
  • dhsto's answer is often useful in a (totally separate, but I'll bring it up anyway) case where you want to minimize the JSON you throw at a view. I often have a json parsing as 1's and 0's rather than "true" and "false". Saves a significant amount of space
    – Stachu
    Jul 17, 2013 at 18:18

6 Answers 6

24

As a boolean (bool), the values will always be "True" or "False". If you want to represent these differently when converted to a string, you can do the following in your view:

@Model.IsAdded.ToString().ToLower()
2
  • 1
    This. Or, something like " ('@(Model.IsAdded)' == 'True') when evaluating.
    – Stachu
    Jul 17, 2013 at 18:17
  • Wow! Too simple solution. I think it was my fault. I need to pay more attention to the type. Thanks, Matt! Jul 17, 2013 at 18:27
4

This will not work as IsAdded is boolean and "true" is string

IsAdded = (sessionStore.CheckExistanceOnSummary(product.productId) ? 
    "true" : 
    "false")

Try using it in javascript by converting it toLowercase().

4

You could create a Display Template for bool that is "true"/"false" for reusability purposes:

  1. Create a Display Template called "JsBoolean.cshtml":

  2. In your view, use @Html.DisplayFor() and specify your new template.

JsBoolean.cshtml:

@model Boolean

@(Model ? "true" : "false")

In your view:

@Html.DisplayFor(m => m.IsAdded, "JsBoolean")

Using this scheme, you have added "reusability" to your views for javascript purposes. You could also use this (if needed) to make booleans display as "yes" or "no" or something else.

Edit:

If your boolean values will always be lowercase, then you can make your custom view the default one by calling it "Boolean.cshtml". Then you can simplify your use to:

@Html.DisplayFor(m => m.IsAdded)
3

You can add an extra read-only property (for example jsIsAdded) which returns IsAdded.ToString().ToLower().

2

Perhaps you could incorporate the following code:

IsAdded.ToString().ToLower();

Sort of like this:

public string JSCheckExistanceOnSummary(Nullable<int> productId)
{
    return  CheckExistanceOnSummary(productId).ToString().ToLower();
}
2

Could you use a bit instead? 0 or 1 this way no matter the langage. Also most of the the bool is most likely a bit (check sql for example)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.