Wrox Programmer Forums
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
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?)
 
Old October 20th, 2010, 12:21 PM
Friend of Wrox
 
Join Date: Oct 2010
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>" %>
 
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>" %>
"
 
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.
 
Old October 21st, 2010, 10:43 AM
Friend of Wrox
 
Join Date: Oct 2010
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.
 
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>
 
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.
 
Old October 25th, 2010, 01:30 PM
Friend of Wrox
 
Join Date: Oct 2010
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.
 
Old October 25th, 2010, 01:35 PM
Friend of Wrox
 
Join Date: Oct 2010
Posts: 106
Thanks: 1
Thanked 17 Times in 17 Posts
Default

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





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 01:07 PM
View Source from ActiveX Browser Object echovue Access 4 November 1st, 2005 01: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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.