Null Reference Exception , But it is NOT a null value

Viewed 60

I yesterday started working on my first .NET c# MVC project. Now I receive a null reference when i try to POST but before that when i try to read from that same value it works fine. What I mean is that when the page loads, it loads the value correctly, but when i press the submit button it throws the exception .

@model QnA_WebApp_MVC.Models.Block
@{
    Block block = (Block)@TempData["block"];
}
<h5>User: @block.user_id</h5>

And the thing is that the block.user_id is not even among the posted values.

1 Answers

Anyway I found the solution my self. I changed the

Block block = (Block)@TempData["block"];

To

Block block = new Block();
block = ViewBag.block;

In a previous attempt i had tried

Block block = ViewBag.block;

and it was not working -> null reference exception

I believe that the solution was that i first declared the block as a new Block() but i am not 100%. Anyway now it works so i am moving on

Related