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)

Was it helpful?

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top