views:

86

answers:

2

Hi

I need to pass in 2 objects to a page to access Model.NewsItems and Model.Links (the first is a class of news item objects with heading, content etc and the Links are a set of strings for hyperlink depending on whether the system is up or down.

This is the page declaration

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master"  
    Inherits="System.Web.Mvc.ViewPage<Tolling.Models.NewsItems>" %>

If I refer to Model.Items - I am fine. However, if I refer to Model.HyperLink1, I am not.

How can you pass in multiple objects into the page? I have tried importing both namespaces without success - i.e.

<%@ Import Namespace="Tolling.Models.News" %>
<%@ Import Namespace="Tolling.Models.HyperLinks" %>
A: 

You can pass extra data into the View by using ViewData, TempData, Session or Cache. I'd suggest you use ViewData. As described by MSDN:

Gets or sets a dictionary that contains data to pass between the controller and the view.

Yuriy Faktorovich
+1  A: 

Create a ViewModel class that contains both of your model collections and then pass that too the view:

Sample Controller:

var myNewModel = new MyNewModel()
{ 
    NewsItems = new List<NewItem>(), 
    HyperLinks = new List<HyperLink>() 
}

return View(myNewModel);

View page declaration:

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master"  
    Inherits="System.Web.Mvc.ViewPage<MyNewModel>" %>

Then you can access them in your view with your new ViewModels properties:

<%= Model.NewsItems %>
<%= Model.Hyperlinks %>
jfar