Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > BOOK: Professional ASP.NET MVC 2
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Professional ASP.NET MVC 2
This is the forum to discuss the Wrox book Professional ASP.NET MVC 2 by Jon Galloway, Scott Hanselman, Phil Haack, Scott Guthrie, Rob Conery; ISBN: Professional ASP.NET MVC 2
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional ASP.NET MVC 2 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Display Modes
  #1 (permalink)  
Old October 20th, 2010, 10:37 AM
Registered User
 
Join Date: Oct 2010
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Cool passing an object to a view

Hi. I'm following your book as my guideline to MVC2 (since i'm learning it).
I had an object and want to passe from a controller to a view.
My object has 3 other objects on it as:
Code:
public class MenuContainer 
    {
        public List<Menu> Containers = new List<Menu>();
    }
    public class Menu
    {
        public string title { get; set; }
        public List<MenuItem> Items = new List<MenuItem>();

    }
    public class MenuItem
    {
        public string description { get; set; }
        public string url { get; set; }
    }
}
Now i neet to pass my final object (MenuContainer ) to a view. On the view i mys get several values from "sub-objecs".
Now... as it has been said on chapter 6, pag. 285, i had this to my view:
Code:
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MenuContainer>" %>
The solution is build but its to be rendered on browser i get this:

Compiler Error Message: CS0246: The type or namespace name 'MenuContainer' could not be found (are you missing a using directive or an assembly reference?)
  #2 (permalink)  
Old October 20th, 2010, 12:21 PM
Friend of Wrox
Points: 539, Level: 8
Points: 539, Level: 8 Points: 539, Level: 8 Points: 539, Level: 8
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2010
Location: Seattle
Posts: 106
Thanks: 1
Thanked 17 Times in 17 Posts
Default

I believe you need to specify full namespace in your view. Look for "namespace" in your class. So your view code should look something like this:

Code:
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<YourProject.Models.MenuContainer>" %>
  #3 (permalink)  
Old October 21st, 2010, 04:46 AM
Registered User
 
Join Date: Oct 2010
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default yes.

you right.

"<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<NerdDinner.Model s.MenuContainer>" %>
"
  #4 (permalink)  
Old October 21st, 2010, 05:45 AM
Registered User
 
Join Date: Oct 2010
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Exclamation iterating an object in view

As i said before, I had this:

Code:
public class MenuContainer 
    {
        public List<Menu> Containers = new List<Menu>();
    }
    public class Menu
    {
        public string title { get; set; }
        public List<MenuItem> Items = new List<MenuItem>();

    }
    public class MenuItem
    {
        public string description { get; set; }
        public string url { get; set; }
    }
}
When i'm trying iterate on view:
Code:
foreach(NerdDinner.Models.MenuContainer xpto in Model) 
                   {%>
i had an error about bad implementation on IENumerator implementation. can someone guide me on this:
Code:
    public class MenuContainer : IEnumerator
    {

        public List<Menu> Containers = new List<Menu>();

        private int Position = -1;

        public IEnumerator GetEnumerator()
        {
            return (IEnumerator)this;
        }

        ///
        public bool MoveNext()
        {
            if (Position < Containers.Count - 1)
            {
                ++Position;
                return true;
            }
            return false;
        }

        public void Reset()
        {
            Position = -1;
        }


        public object Current
        {
            get
            {
                if (Position < 0)
                    throw new InvalidOperationException();
                if (Position > Containers.Count)
                    throw new InvalidOperationException();
                return this.Containers;
            }
        }
    }
    
    /// <summary>
    /// Classe que contém sub-menu (e os submenu itens relativos a estes)
    /// </summary>
    public class Menu 
    {
        public string title { get; set; }
        public List<MenuItem> Items = new List<MenuItem>();
    }

    /// <summary>
    /// Classe que representa os items dos sub-menus - descrição e url (como é mvc isto deverá ser alterado para reflectir
    ///  uma view numa fase posteriormente
    /// </summary>
    public class MenuItem
    {
        public string description { get; set; }
        public string url { get; set; }
    }
I need to iterate all objects in order to get their values in my value.
One step at the time, but i'm stuck arounde here now.
  #5 (permalink)  
Old October 21st, 2010, 10:43 AM
Friend of Wrox
Points: 539, Level: 8
Points: 539, Level: 8 Points: 539, Level: 8 Points: 539, Level: 8
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2010
Location: Seattle
Posts: 106
Thanks: 1
Thanked 17 Times in 17 Posts
Default

So far this code looks OK. I think the problem may be somewhere else. Show us your Model code.
  #6 (permalink)  
Old October 25th, 2010, 12:56 PM
Registered User
 
Join Date: Oct 2010
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default my bad

my first code was wrong. to iterate the right code is this:

<ul class="dropdownmenu">
<% foreach (NerdDinner.Models.Menu m in Model.Containers)
{%>
<li class="mainmenu">
<div id="xpto"><%: m.title %></div>
<ul class="submenu" id="xpto2">
<%foreach (NerdDinner.Models.MenuItem mItem in m.Items)
{%>
<li>
dasda
<%: Html.ActionLink(mItem.description,mItem.view, mItem.controler ) %>

</li>
<% } %>
</ul>
</li>
<%}%>
</ul>
  #7 (permalink)  
Old October 25th, 2010, 01:07 PM
Registered User
 
Join Date: Oct 2010
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default logical doubt passing objects to site.master

After i get this work. i realize that, since i want to use this object as root for a menu, the iteration on object must be done outside a view, since, if i get from the view, i'll lost the object passed to it by parameter.

I had two diferente ideias but got stuck on both. First was to use a partial view (i didn't get to have any reference to my xml object there) - why?!

My second try was to pass the object to a viewdata and called it on site.master directly. Which is the best aproach?

I follow the 2nd one and got stuck, since i can't convert from the viewdata object to my object it self, or i can but having this error:
" Message=Unable to cast object of type 'NerdDinner.Models.MenuContainer' to type 'System.Collections.Generic.IEnumerable`1[NerdDinner.Models.MenuContainer]'.
"

This happens when on my site.master i do this:
Code:
    <% foreach (NerdDinner.Models.MenuContainer varc in (IEnumerable<NerdDinner.Models.MenuContainer>)ViewData["XmlDoc"])
                   {%> 

                   
                   <%}
                           
                           
                %>
I had posted a similar doubt on asp.net foruns, btw.
  #8 (permalink)  
Old October 25th, 2010, 01:30 PM
Friend of Wrox
Points: 539, Level: 8
Points: 539, Level: 8 Points: 539, Level: 8 Points: 539, Level: 8
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2010
Location: Seattle
Posts: 106
Thanks: 1
Thanked 17 Times in 17 Posts
Default

Seems beyond the scope of this forum. Maybe your ViewData["XmlDoc"] isn't really a MenuContainer? Can't tell because we have no idea what you did to set this in the controller.
  #9 (permalink)  
Old October 25th, 2010, 01:35 PM
Friend of Wrox
Points: 539, Level: 8
Points: 539, Level: 8 Points: 539, Level: 8 Points: 539, Level: 8
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2010
Location: Seattle
Posts: 106
Thanks: 1
Thanked 17 Times in 17 Posts
Default

Oh, might not be casting correctly. See this example.
 


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
passing an object to a manipulator function varun C++ Programming 1 April 7th, 2006 02:24 AM
Passing jagged array to com object nutrino VB How-To 0 January 27th, 2006 12:07 PM
View Source from ActiveX Browser Object echovue Access 4 November 1st, 2005 12:13 PM
Passing Class object to WS r_ganesh76 .NET Web Services 4 October 4th, 2004 11:12 PM
passing an object to the dropdownlist jaisonkmathews General .NET 1 September 27th, 2004 11:44 AM



All times are GMT -4. The time now is 06:31 PM.


Powered by vBulletin®
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.