Return true
instead of True
from Controller to View.
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.
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'
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;
}
I read this topic, but didn't not help me.
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 !=
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).
The expressions true == 1 and false == 0 are both true. (And true == 2 is not true).
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With