Passing objects from controller to viewhttp://forums.asp.net/t/1230383.aspx/1?Passing+objects+from+controller+to+viewMon, 10 Mar 2008 09:44:23 -040012303832219014http://forums.asp.net/p/1230383/2219014.aspx/1?Passing+objects+from+controller+to+viewPassing objects from controller to view <p>Hi there!</p> <p>&nbsp;I've been developing web-applications in RoR for a while and saw that .NET now had support for MVC based applications. So I started to fiddle around some with the examples and passing simple data such as strings and integers via a controller to its corresponding view works like a charm. But trying to pass an object.. not so charming. </p> <p>This is my controller:</p> <p>&nbsp;<pre class="prettyprint">using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace MvcApplication1.Controllers { public class SampleViewData { public string Name { get; set; } public int AnInt { get; set; } } public class LoginController : Controller { public void Login() { RenderView(&quot;Login&quot;); } public void FormSubmit(string name, string password) { // TODO: Authenticate user //ViewData[&quot;Name&quot;] = name; //ViewData[&quot;AnInt&quot;] = myInt; //RenderView(&quot;Welcome&quot;); SampleViewData myData = new SampleViewData(); myData.Name = name; myData.AnInt = 1; ViewData[&quot;MyObject&quot;] = myData; RenderView(&quot;Welcome&quot;); } } }</pre> <P>&nbsp;And this is my Welcome.aspx view:</P> <P mce_keep="true">&nbsp;<pre class="prettyprint"><SPAN class=dir>&lt;%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" AutoEventWireup="true" CodeBehind="Welcome.aspx.cs" Inherits="MvcApplication1.Views.Login.Welcome" %&gt;</SPAN> &lt;<SPAN class=tag>asp:Content</SPAN><SPAN class=attr> ID=</SPAN><SPAN class=attrv>"Content1"</SPAN><SPAN class=attr> ContentPlaceHolderID=</SPAN><SPAN class=attrv>"MainContentPlaceHolder"</SPAN><SPAN class=attr> runat=</SPAN><SPAN class=attrv>"server"</SPAN>&gt; &lt;<SPAN class=tag>h2</SPAN>&gt;Welcome&lt;/<SPAN class=tag>h2</SPAN>&gt; &lt;<SPAN class=tag>p</SPAN>&gt;Welcome, <SPAN class=dir>&lt;%=</SPAN> ViewData["MyObject"].Name <SPAN class=dir>%&gt;</SPAN>&lt;/<SPAN class=tag>p</SPAN>&gt; &lt;/<SPAN class=tag>asp:Content</SPAN>&gt;</pre> </p> <p>&nbsp;However.. this generates the following error:</p> <p>&nbsp;<strong>Kompilatorfelmeddelande: </strong>CS1061: 'object' does not contain a definition for 'Name' and no extension method 'Name' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)<br> <br> if i just use this: ViewData[&quot;MyObject&quot;] it prints that it is a&nbsp; MvcApplication1.Controllers.SampleViewData object.</p> <p>I also tried to pass the object as: RenderView(<span class="st">&quot;Welcome&quot;, myData</span>); and print it as: ViewData.Name but no variation of this works.</p> <p>Some help on how to resolve this problem would make me a very happy programmer. Please be as basic as you can with your explanation as I'm a total noob in the world of .NET :P</p> <p><br> &nbsp;</p> <p>&nbsp;</p> 2008-03-07T14:30:14-05:002219090http://forums.asp.net/p/1230383/2219090.aspx/1?Re+Passing+objects+from+controller+to+viewRe: Passing objects from controller to view <p>Pass the object to the View like this : RenderView(<span class="st"><font color="#ff0000">&quot;Welcome&quot;,MyData</font></span>);</p> <p>&nbsp;Then Strongly type the viewpage (Go to the code behind and instead of inheriting from viewpage change that to ViewPage&lt;SampleViewData&gt; Where SampleViewData is your object type above. You can now access your objece in the Page like ViewData.Property<br> </p> 2008-03-07T14:58:13-05:002219164http://forums.asp.net/p/1230383/2219164.aspx/1?Re+Passing+objects+from+controller+to+viewRe: Passing objects from controller to view <p>C# is a statically typed language, Ruby is dynamically typed (meaning you can access a property, and it will work for any type as long as it has a property with that name). In C# you have to cast to the original type from object. This can be hard to get used to when switching over.<br> </p> <p>So <b>((SampleViewData) ViewData[&quot;MyObject&quot;]).Name</b> will get do the cast and return the Name.</p> <p>Like mrfleck says, there is a strongly typed version of ViewPage, in which ViewData IS the type you specify so you can just do <b>ViewData.Name</b>. Much easier to understand than casting.<br> </p> <p>&nbsp;</p> <p>However, you will be able to use MVC with IronRuby (a dynamically typed language for the .NET Framework). I believe Scott Hanselman will be the guy to go to on this. He did a presentation of MVC with IronPython at ALT.NET last year. <a href="http://download.microsoft.com/download/f/0/8/f0830f07-44db-4eea-ace3-8865856c8d65/ScottHaOnDLRandMVCatALTNET.wmv"> http://download.microsoft.com/download/f/0/8/f0830f07-44db-4eea-ace3-8865856c8d65/ScottHaOnDLRandMVCatALTNET.wmv</a></p> <p>&nbsp;</p> 2008-03-07T15:23:26-05:002222421http://forums.asp.net/p/1230383/2222421.aspx/1?Re+Passing+objects+from+controller+to+viewRe: Passing objects from controller to view <p>Sweet! now everything is working as it should. </p> <p>&nbsp;Don't think I'll try making web-applications in IronRuby, but it's good to know that the IronRuby project exist. The coding standard at my current company is C# so we are gonna try and use that as much as possible.</p> <p>But with the MVC framework.. most things feel very &quot;rubyesque&quot; so I think I will feel at home after a couple of weeks anyways ;)</p> <p>&nbsp;</p> <p>Thanks for your help!</p> 2008-03-10T09:37:30-04:002222434http://forums.asp.net/p/1230383/2222434.aspx/1?Re+Passing+objects+from+controller+to+viewRe: Passing objects from controller to view <p>&nbsp;don't forget to mark the posts that helps as answers<br> </p> 2008-03-10T09:44:23-04:00