I am using MVC with LINQ-to-SQL class.
as foreign key can be null, i have one record having f.k. null and others are having values.
now i am displaying it in view by Index view.
In index view i am resolving f.k. by writing code like
<%= Html.Encode(item.UserModified.UserName) %>
Now i have a problem in view that ‘object reference is not set’.
And this is because of we are having null value in one of the f.k. field!
so can i write code in view to check whether Associated object pointing to null or nothing?
You can write any code you want in the view if necessary, so you could do:
You could also make a HtmlHelper extension to do this:
You could then simply do:
Of course if it’s UserModified that’s null and not UserName then you’ll need some different logic.