0

I have a view that contains a variable i am getting out of the ViewData dictionary. The code looks like

@{
    ParentModel parent = (ParentModel)ViewData["Parent"];
}

I though ok, its becuase the object does not exist in the ViewData Dictionary so i should soft cast it and check to see if it is null before using it. So i changed it to

@{
    ParentModel parent = ViewData["Parent"] as ParentModel;
}

but i still get a null reference exception on this line. So to test something i changed it to

@{
    ParentModel parent = new ParentModel();
}

and it still throws an exception. What on earth is going on, why would instantiating an object in a view throw a null reference exception?

Update

I removed an unused reverence at the top of the view which resharper was letting me know of and the next using reference displayed this error

"there is no build provider registered for the extension cshtml"

I think something is wrong with ASP.Net, looking online some people suggest adding something to the Web.config, but i checked version control and this has not changed, why would this issue just start happening

3
  • 1
    can you show complete code may be some line before this one is throwing as this line should not throw exception Commented Jun 10, 2014 at 7:46
  • 3
    you may be having a another class dependency(other class) in ParentModel class that you are not initializing. Commented Jun 10, 2014 at 7:47
  • can you add code for your ParentModel ? Commented Jun 10, 2014 at 7:58

2 Answers 2

1

If you get a null reference exception in this line:

@{
    ParentModel parent = ViewData["Parent"] as ParentModel;
 }

Then the ViewData it self is null. You have to instantiate the ViewData before using it.

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

3 Comments

That doesn't explain why i also get a null reference exception in the 3rd code block i tried. It doesn't even use ViewData and MVC should handle this.
The is something in the ParentModel constructor, a dependency which you have to stub or mock. What is the code in the constructor?
It has no explicit constructor and does not inherit any other class
0

It turned out to be caused the model itself being null that was being used in a display template view, the model being completely different to the Parent object that was being retrieved from the ViewData and the line where the exception was being thrown.

I'm not sure why VS would break on the wrong line, but it could be because the this code block was the very first list after the model declaration. for example

@using MyProject.Models
@model ItemModel  <---- VS2010 should break here
@{
    ItemModel parent = ViewData["Parent"] as ItemModel; <-----VS2010 Breaks here
}
<h2>@Model.Title</h2>

Comments

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.