0

I have a problem to convert TempData["Id"] to int. I assign an int value to the TempData["Id"], and want to use it as int. Here is my code:

 TempData["Id"] = 1;
 int Id= (int)TempData["Id"];

But it didn't work. The Id is null. I try to use some suggestion like

Convert.ToInt32(TempData["Id"];
Convert.ToInt32(TempData["Id"].ToString());

But it also shows null. Please help

yusry
  • 148
  • 2
  • 15
  • 7
    We have no idea what type `TempData` is - you've given us no context. Please provide a [mcve] or we won't be able to help you. – Jon Skeet Sep 27 '17 at 07:25
  • please provide code that is easy to copy-paste. it should be able to run after a single copy paste. – FrankK Sep 27 '17 at 07:26
  • @MongZhu: From the example it is impossible to tell what type the id is. It could be `object`, which is more likely because of the following typecast. – Sefe Sep 27 '17 at 07:29
  • I want to pass the int value 1 through tempdata. So i write like this TempData["Id"] = 1; – yusry Sep 27 '17 at 07:32
  • @yusry post your entire code. If we don't know what type TempData is we cant help you – FrankK Sep 27 '17 at 07:32
  • @yusry but still you don't show how you instantiate TempData what type is it? Is it a Dictionary ? – rmjoia Sep 27 '17 at 07:33
  • @MongZhu im pretty sure he was talking about TempData["Id"] being null – FrankK Sep 27 '17 at 07:34

0 Answers0