Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

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

0.00/5 (No votes)
27 Mar 2013 3  
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;}
} 

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here