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;}
}