Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

true instead of True (C#)

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.

like image 295
Guilherme Oderdenge Avatar asked Jul 17 '13 18:07

Guilherme Oderdenge


People also ask

Is != True the same as == false?

x == false implies x != true , but x != true does not always imply x == false because x can also be some nonsense value. 1 + 1 = 3 is both == false and !=

Is 1 True or false C?

Initial implementations of the language C (1972) provided no Boolean type, and to this day Boolean values are commonly represented by integers ( int s) in C programs. The comparison operators ( > , == , etc.) are defined to return a signed integer ( int ) result, either 0 (for false) or 1 (for true).

Is 2 considered true in C?

The expressions true == 1 and false == 0 are both true. (And true == 2 is not true).


1 Answers

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()
like image 157
Matt Houser Avatar answered Sep 19 '22 18:09

Matt Houser