Confusing on viewdata and model

Topics: Developer Forum
Sep 27, 2008 at 8:45 AM
I want to make a page allow some zones like start page.
  1. I made a model calls Events (inherit AbstractPage and have an available zone "Left")
  2. I made a page in view layer (inherit ViewPage<EventsViewData>)
  3. I made a EventsController in controller layer.([Controls(typeof(Models.Events))] and inherit ContentController<Models.Events>)
  4. I made a EventsViewData in the view layer at the same folder as page (inherit AbstractPage).
  5. I want to put these code into Events.aspx for allow children contentItem.
    1. <% foreach (N2.ContentItem part in ViewData.Model.GetChildren("Left")){ %>
             <% Html.RenderPartial(part.TemplateUrl, part); %>
      <% }%>
  6. When I added a usercontrol in left zone, nothing happend. because ViewData.Model.GetChildren("Left").Count = 0, but I still saw data in database wich I just added. Why?

 

Coordinator
Sep 27, 2008 at 12:24 PM
How did you add the "usercontrol" to the left zone? Is it events you  added through the editing interface to the left zone? What GetChildren("Left") basically does is enumerating Children and return those whose item.ZoneName == "Left"
Sep 28, 2008 at 3:11 AM
Is it events you  added through the editing interface to the left zone?
A: Yes, I chosen the zone "Left" on tab zone and then chosen the usercontrol on tab type.
I am sure zone name is "Left", ViewData.Model.GetChildren("Left"), the ViewData is EventsViewData.cs but not Model.Events.cs, Am I correct?
Coordinator
Sep 28, 2008 at 10:00 PM
I'm a bit confused by your usage of EventsViewData. It might be the cause of your problem. This class should not inherit any content item class since it's just a messageboard from the controller to the view. Say you do something like this:

ViewData:
public class EventsViewData
{
    public Model.Events CurrentEvent {get;set;}
    public IEnumerable<ContentItem> LeftItems {get;set;}
}

Controller:
public class EventController
{
    public Index()
    {
         return RenderView("index", new EventsViewData
        {
            CurrentEvent = CurrentPage,
            LeftItems = CurrentPage.GetChildren("Left")  // I think it's better if the view doesn't know how to get the left items from the event
        });
    }
}

View:
<% foreach (N2.ContentItem part in ViewData.Model.LeftItems){ %>
       <% Html.RenderPartial(part.TemplateUrl, part); %>
<% }%>