将2个模型传递给可能的视图?

问题描述:

我有一个观点,必须呈现两份报告。将2个模型传递给可能的视图?

我的动作:

public ActionResult TradeUKKPIShowData(SundaysInMonthViewModel model) //show actual data in the view 
{ 
    var tradesmenReportData = _reportingService.GetTradeUKKPITradesmen(model.SelectedSunday); 
    var jobSortedReportData = _reportingService.GetTradeUKKPIJobSorted(model.SelectedSunday); 
    ViewBag.jobSortedReportDate = model.SelectedSunday; 
    ViewBag.addJobSortedModel = jobSortedReportData; 
    return View(tradesmenReportData); 
} 

我的第一个问题是与上述动作的前两行。我必须通过什么? 编译错误,我得到的那些线:

“TradeUK.Services.IReportingService.GetTradeUKKPITradesmen(TradeUK.Entities.Reporting.KPIResults)”有一些无效参数

所有报告中的存储过程,需要的是日期被选择的/周日

我SundaysInMonthViewModel:

namespace TradeUK.Admin.Web.ViewModels 
{ 
    public class SundaysInMonthViewModel 
    { 
     public IEnumerable<SelectListItem> AllSundays { set; get; } 
     public string SelectedSunday { set; get; } 
    } 
} 

这里IReportingServices.cs:

IEnumerable<KPIResults> GetTradeUKKPITradesmen(DateTime date); 
IEnumerable<KPIResults> GetTradeUKKPIJobSorted(DateTime date); 

和实际方法上面:

public IEnumerable<KPIResults> GetTradeUKKPITradesmen(DateTime date) 
{ 
} 


public IEnumerable<KPIResults> GetTradeUKKPIJobSorted(DateTime date) 
{ 
} 

KPIResults:

namespace TradeUK.Entities.Reporting 
{ 
public class KPIResults 
    { 
    public virtual string Title { get; set; } 
    public virtual int Total { get; set; } 
    public virtual int Week6 { get; set; } 
    public virtual int Week5 { get; set; } 
    public virtual int Week4 { get; set; } 
    public virtual int Week3 { get; set; } 
    public virtual int Week2 { get; set; } 
    public virtual int Week1 { get; set; } 
    } 
} 

另一个问题是对回报(查看(tradesmenReportData,jobSortedReportData) 我能做到这一点?在实际的视图中,我必须对循环进行foreach以显示各组报告数据。

请帮忙? 感谢

+0

什么是IReportingService.GetTradeUKKPITradesmen'的'的定义,什么是'model.SelectedSunday'的类型?不知怎的,我不认为后者是'TradeUK.Entities.Reporting.KPIResults'。 – 2012-08-16 08:37:17

可以正常和第二通过第一种模式中ViewBag然后

@model YourFirstModelClass 

@{ 
    YourSecondModelClass model2 = ViewBag.YourSecondModel; 
} 

而且有了它你有你想要的效果。

所以,当你不使用剃须刀,你可以添加主强类型模型是这样的:

<%@ Inherits="System.Web.Mvc.ViewPage<PrimaryModelClass>" %> 

和第二位的ViewData与之相似,以ViewBag

+0

我在OP中添加了我的视图代码,这是正确的吗?我仍然处于学习过程中,并且还不知道如何。我不使用剃须刀。谢谢 – 2012-08-16 09:00:48

+0

我使用一个模型为两个报告他们有相同的化妆,所以在我的情况下,它会是SundaysInMonthViewModel?谢谢 – 2012-08-16 09:21:22

+0

不客气,标记答案为接受或upvote,如果它有帮助。 – 2012-08-16 09:24:50

关于你提到的“第二个问题”:

不,你不能这样做,只有一个ModelViewData。您应该为这种情况创建单独的视图模型(或使用诸如Tuple之类的东西)。

关于你的第二个问题,你可以创建一个模型视图类与两个表的属性'栏。从这个模型类中,您可以查看查看页面中的数据并将数据保存在特定的表格中。

我在网上找到了这个链接,可以为你做。

https://weblogs.asp.net/dotnetstories/passing-multiple-models-to-a-view-in-an-asp-net-mvc-5-0-application

编辑: -

我已经链接到建议传递对象的列表来查看,与列表正在由外径不同的模式,这里的页面的例子。

public ActionResult Index() 
    { 
     List<object> model = new List<object>(); 
     model.Add(ctx.ProductCategories.ToList()); 
     model.Add(ctx.Products.ToList()); 

     return View(model); 
    } 
} 

然后在视图中,您可以再次向下突破的列表个别型号,这里笔者给出的例子: -

@model IEnumerable<object> 

@{ 

    List<PassingMultipleModelsToAView.ProductCategory> lstPCategory = Model.ToList()[0] as List<PassingMultipleModelsToAView.ProductCategory>; 

    List<PassingMultipleModelsToAView.Product> lstProduct = Model.ToList()[1] as List<PassingMultipleModelsToAView.Product>; 
} 

<h3>Categories</h3> 

<ul> 
    @foreach (var item in lstPCategory) 
    {  
     <li>@item.Name</li> 
    } 
</ul> 

<hr /> 

<h3>Products</h3> 

<ul> 
    @foreach (var item in lstProduct) 
    { 
     <li>@[email protected]</li> 
    } 
</ul>