65.9K
CodeProject is changing. Read more.
Home

How to fix the 'object' does not contain a definition for error

Mar 27, 2013

CPOL
viewsIcon

75070

When building a View with a ViewBag containing a property that references a class.

One reason to get 'object' does not contain a definition for...'any property name' exception is that the class you referenced in the ViewBag dynamic property you added is a protected or private class.

Remember that if you don't mark the class, it will be always protected.

The ViewBag needs to access the class from a public application domain, not from protected.

This class will fail if you call ViewBag.Person.Name with the error  'object' does not contain a definition for...'Name' 

class Person {
public string Name { get; set; }
public string Surname {get; set; }
}

But if you write public before the class, all will be OK.

public class Person {
public string Surname {get; set; }
public string Name { get; set;}
}