0

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'.
4
  • It seems like razor means item.Id is a method group Commented Jul 8, 2015 at 9:08
  • Can you put the full view? Commented Jul 8, 2015 at 9:09
  • public string Id { get; set; } Commented Jul 8, 2015 at 9:11
  • 1
    What's the value of item.Id? Commented Jul 8, 2015 at 9:12

3 Answers 3

5

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

Sign up to request clarification or add additional context in comments.

7 Comments

Hi Jitendra, but your first example is still giving an error with string as id. And also with the cast in it.
Yes, In that I am showing that if you take Id as string it's gives error, so you have to change it to int which is in second example.
I added one more fiddle with Id taking as string. Have a look & let me know.
Hi Jitendra, yes, thank you. But see my post. I had that
I get then the following error: Unable to cast object of type 'System.Int32' to type 'System.String'.
|
2

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.

4 Comments

If it were a string the error message would be "[...] to operands of type 'string' and 'int' [...]"
@greenhoorn in the comments the OP identifies it as a string.
Yes, sorry. I wrote the comment before he did :D
I Edit the the post> I removed the cast
2

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

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

1 Comment

remove the semicolon and try again I just posted the sample code. please modify according to your requirement.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.