开发者

MVC Multiple ViewPage items in aspx required

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 开发者_如何学Gopage 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" %>


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 %>


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜