InfinityGoesAround
InfinityGoesAround

Reputation: 1021

strange error in razor

I have this:

<tr class="@(item.Id == (int)(Session["Id"] ?? 0) ? 
   "sfs-selected sfs-selectable" : String.Empty)">

but I get this meesage:

operator '==' cannot be applied to operands of type 'method group' and 'int'

but I already cast to int.

If I do this:

 <tr class="@if (item.Id == (string)(Session["id"] )) {@("sfs-selected sfs-selectable") } @string.Empty ">

then I get this error:

Unable to cast object of type 'System.Int32' to type 'System.String'.

So how to check on null value?

Thank you

if I do this:

 <tr class="@(item.Id == (Session["Id"] ?? 0) ? "sfs-selected sfs-selectable" : String.Empty)">

I get this warning:

Warning as Error: Possible unintended reference comparison; to get a value comparison, cast the right hand side to type 'string'

So I do this:

<tr class="@(item.Id == (string)(Session["Id"] ?? 0) ? "sfs-selected sfs-selectable" : String.Empty)">

then I get this:

Unable to cast object of type 'System.Int32' to type 'System.String'.

Upvotes: 0

Views: 157

Answers (3)

BSG
BSG

Reputation: 2104

If you do not want to make id as int then change like this and try

(item.Id == (Session["Id"].ToString() ?? "0") ?

Upvotes: 2

Jitendra Pancholi
Jitendra Pancholi

Reputation: 7552

You should either remove (int) type cast or make Id as int. see this fiddle.

https://dotnetfiddle.net/1LmsTm // Showing Error

https://dotnetfiddle.net/ka4Y59 // Changing ID to int from string

https://dotnetfiddle.net/LhsiM3 // removing int from type cast with ID as string

Upvotes: 5

Ronald Meijboom
Ronald Meijboom

Reputation: 1564

The Id in item is a string and that is why you get this message. Remove the cast of the int or change the type of Id to int.

Upvotes: 2

Related Questions