Click here to Skip to main content
15,881,938 members
Articles / Web Development / ASP.NET
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
27 Mar 2013CPOL 73.7K   1   2
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' 

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

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

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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead Get Me An Apartment
Spain Spain
I'm developer fall in love with C#, .NET and Mono.
Designing and programming websites since 2001.
I like literature (to read and to write), playing football (soccer)... and of course I'm great fan of La Roja (Spain football team)

At now I work in GetMeAnApartment, I do Research and Development features to upgrade our website (asp.net, MVC, MySQL) and business model (rental short stay apartments)


My Knowledge:
LANGUAGES: C#, XML, HTML, JavaScript, ActionScript, php, VisualBasic Script

TECHNOLOGIES: asp.net, asp.net mvc, php, flash, GTK#

APPLICATIONS: wordpress, wikimedia, I programmed modules for Macromedia Flash MX for myself

FRAMEWORKS: .Net Framework, Mono

SERVERS: IIS, apache2

SO: Windows, Debian

SOFTWARE: Visual Studio 2010, 2008. Monodevelop. Glade. Illustrator. Indesign. Photoshop. Dreamweaver.


Employment:

GetMeAnApartment
IT Director and website developer, 2010 - present

Círculo Rojo Publicidad y Comunicación
IT Director and websites developer, 2007 - 2010

Dédalo Creative
Co-founder,Programmer and Developer, Graphical designer 2006 - 2007

Costa Mediterránea Comunicación
Chief writer, 2004 - 2006

Stalker Creacion
Web designer and developer, 2001 - 2003

Education
Myself
Designer and Programmer, 2001 - present

Jaime I, University
Advertising, 2000 - 2005

Comments and Discussions

 
Questionorder matters? Pin
Member 1089674020-Jun-14 5:01
Member 1089674020-Jun-14 5:01 
AnswerRe: order matters? Pin
AlbertoLeon CSharpMan20-Jun-14 5:11
AlbertoLeon CSharpMan20-Jun-14 5:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.