How to pass an integer value using ViewData in asp.net mvc and then use value stored in ViewData to compare with another integer value in the view?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



How to pass an integer value using ViewData in asp.net mvc and then use value stored in ViewData to compare with another integer value in the view?



For eg.



I want to do something like this in the controller :


ViewData["dummy"] = 100;



and then in the view I wish to compare its value like :


@if(ViewData["dummy"] = 100)

// do some stuff



Currently if I do this, I get an error saying -


Operator "==" cannot be applied to operands of type 'object' and 'int'





Replace @if(ViewData["dummy"] = 100) to @if(Convert.ToInt32(ViewData["dummy"]))==100
– Ole EH Dufour
8 mins ago






@OleEHDufour It still gives me a run-time exception saying - InvalidCastException: Unable to cast object of type 'Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1[System.Int32]' to type 'System.IConvertible'.
– oneinaminion
4 mins ago





1 Answer
1



Try
if((int)ViewData["dummy"] == 100)


if((int)ViewData["dummy"] == 100)



but I would also add a null check and some kind of "safe casting helper method"






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard