I have a Razor view that starts as:
@using My.Models @model MySpecificModel @{ ViewBag.Title = "My Title"; // NullReferenceException here: string dateUtc = (Model == null ? DateTime.UtcNow.ToShortDateString() : Model.Date.ToShortDateString());
I see no reason to throw a NullReferenceException on the last line (note: the material "= ?:" is on the same line in my source code. It is formatted to be here.)
Then I delete the declaration / assignment in dateUtc , and the NullReferenceException throws up the ViewBag.Title line:
@using My.Models @model MySpecificModel @{ ViewBag.Title = "My Title";
How can this happen? ViewBag , of course, is not null.
NOTE 1 : This only happens if Model is null.
NOTE 2 : MySpecificModel.Date is of type DateTime, so it can never be null.
source share