You Device
is null and throws exception
Try this:
@Model.Where(x=> x.Device != null).Count(x=> x.Device.VehicleReg)
Question
I am counting table rows in MVC view.
The below code was counting from a linked table but has now stoped giving me the following error:
Object reference not set to an instance of an object.
Has anyone any idea as to why this would happen?
@Model.Count(x => x.Device.VehicleReg != null)
Solution
You Device
is null and throws exception
Try this:
@Model.Where(x=> x.Device != null).Count(x=> x.Device.VehicleReg)
OTHER TIPS
Assuming that Model
is not null, probably Device
is null
that's why you get the "Object reference " error