How do I reference members of a single object passed to the View?

Posted by Juxtaposed on Stack Overflow See other posts from Stack Overflow or by Juxtaposed
Published on 2010-06-17T00:26:13Z Indexed on 2010/06/17 0:32 UTC
Read the original article Hit count: 238

Filed under:
|
|

I'm new to MVC2 in ASP.NET/C#, so please forgive me if I misunderstand something. I have code similar to this in my Controller:

var singleInstance = new Person("John");
ViewData["myInstance"] = singleInstance;
return View();

So in my view, Index.aspx, I want to be able to reference members in that object. For example, Person has a member called Name, which is set in the constructor. In the view I want to get Person.Name from what is stored in the ViewData object. Ex.:

<%= ViewData["myInstance"].name %>

That doesn't work. The only real workaround I've found is to do something like this:

<% var thePerson = ViewData["myInstance"];
print (or whatever the method is) thePerson.Name;
%>

Any help would be much appreciated... This was so much easier in PHP/Zend Framework... sigh

© Stack Overflow or respective owner

Related posts about c#

Related posts about mvc