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.
This is my controller:
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("Login");
}
public void FormSubmit(string name, string password)
{
// TODO: Authenticate user
//ViewData["Name"] = name;
//ViewData["AnInt"] = myInt;
//RenderView("Welcome");
SampleViewData myData = new SampleViewData();
myData.Name = name;
myData.AnInt = 1;
ViewData["MyObject"] = myData;
RenderView("Welcome");
}
}
}
Kompilatorfelmeddelande: 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?)
if i just use this: ViewData["MyObject"] it prints that it is a MvcApplication1.Controllers.SampleViewData object.
I also tried to pass the object as: RenderView("Welcome", myData); and print it as: ViewData.Name but no variation of this works.
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
Pass the object to the View like this : RenderView("Welcome",MyData);
Then Strongly type the viewpage (Go to the code behind and instead of inheriting from viewpage change that to ViewPage<SampleViewData> Where SampleViewData is your object type above. You can now access your objece in the Page like ViewData.Property
Marked as answer by kombatsanta on Mar 10, 2008 10:34 AM
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.
So ((SampleViewData) ViewData["MyObject"]).Name will get do the cast and return the Name.
Like mrfleck says, there is a strongly typed version of ViewPage, in which ViewData IS the type you specify so you can just do
ViewData.Name. Much easier to understand than casting.
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.
But with the MVC framework.. most things feel very "rubyesque" so I think I will feel at home after a couple of weeks anyways ;)
kombatsanta
Member
6 Points
8 Posts
Passing objects from controller to view
Mar 07, 2008 02:30 PM|LINK
Hi there!
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.
This is my controller:
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("Login"); } public void FormSubmit(string name, string password) { // TODO: Authenticate user //ViewData["Name"] = name; //ViewData["AnInt"] = myInt; //RenderView("Welcome"); SampleViewData myData = new SampleViewData(); myData.Name = name; myData.AnInt = 1; ViewData["MyObject"] = myData; RenderView("Welcome"); } } }And this is my Welcome.aspx view:
However.. this generates the following error:
Kompilatorfelmeddelande: 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?)
if i just use this: ViewData["MyObject"] it prints that it is a MvcApplication1.Controllers.SampleViewData object.
I also tried to pass the object as: RenderView("Welcome", myData); and print it as: ViewData.Name but no variation of this works.
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
mrfleck
Member
164 Points
109 Posts
Re: Passing objects from controller to view
Mar 07, 2008 02:58 PM|LINK
Pass the object to the View like this : RenderView("Welcome",MyData);
Then Strongly type the viewpage (Go to the code behind and instead of inheriting from viewpage change that to ViewPage<SampleViewData> Where SampleViewData is your object type above. You can now access your objece in the Page like ViewData.Property
tgmdbm
Contributor
4392 Points
883 Posts
ASPInsiders
MVP
Re: Passing objects from controller to view
Mar 07, 2008 03:23 PM|LINK
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.
So ((SampleViewData) ViewData["MyObject"]).Name will get do the cast and return the Name.
Like mrfleck says, there is a strongly typed version of ViewPage, in which ViewData IS the type you specify so you can just do ViewData.Name. Much easier to understand than casting.
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. http://download.microsoft.com/download/f/0/8/f0830f07-44db-4eea-ace3-8865856c8d65/ScottHaOnDLRandMVCatALTNET.wmv
kombatsanta
Member
6 Points
8 Posts
Re: Passing objects from controller to view
Mar 10, 2008 09:37 AM|LINK
Sweet! now everything is working as it should.
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.
But with the MVC framework.. most things feel very "rubyesque" so I think I will feel at home after a couple of weeks anyways ;)
Thanks for your help!
tgmdbm
Contributor
4392 Points
883 Posts
ASPInsiders
MVP
Re: Passing objects from controller to view
Mar 10, 2008 09:44 AM|LINK
don't forget to mark the posts that helps as answers