2009-10-29 151 views
0

我该如何着手将两组数据传递给ASP.NET MVC中的视图?如何将两组数据传递给视图?

我已经尝试了几件事,都没有工作,所以我来简单的结论:我做错了。

我有2个查询:

callRepository.FindOpenCalls()callRepository.FindAllMyCalls(user)

,我想通过2的局部视图,以出放两组数据的一个视图(OpenCalls.ascx和AssignedCalls.ascx分别地)。

我想在我的CallsController.vb中使用Index()函数来做到这一点。

目前我有:

' 
' GET: /Calls/ 

<Authorize()> _ 
Function Index() As ActionResult 
    ViewData("OpenCallCount") = callRepository.CountOpenCalls.Count() 
    ViewData("UrgentCallCount") = callRepository.CountUrgentCalls.Count() 
    ViewData("HighCallCount") = callRepository.CountHighCalls.Count() 
    ViewData("NormalCallCount") = callRepository.CountNormalCalls.Count() 
    ViewData("LowCallCount") = callRepository.CountLowCalls.Count() 

    ViewData("MyOpenCallsCount") = callRepository.CountMyOpenCalls(Session("LoggedInUser")).Count() 
    ViewData("UserName") = Session("LoggedInUser") 

    Dim viewOpenCalls = callRepository.FindAllOpenCalls() 
    Dim viewMyOpenCalls = callRepository.FindAllMyCalls(Session("LoggedInUser")) 

    Return View(viewOpenCalls) 
End Function 

这显然只返回开电话,但我想退都viewOpenCalls和viewMyOpenCalls。

我该怎么办呢?

会显示我的LINQ帮助?

感谢您提前提供任何帮助。

回答

2

将数据传递给视图的最佳方式是为视图实际包含特定的ViewData,仅包含所需的数据。

而不必魔术字符串(ViewData("MyOpenCallCount"))定义包含需要该视图中的所有数据一个特定的类(抱歉,如果我的VB.Net是有点生疏):

public class CallInfo 
    public OpendCallCount as int 
    public UrgentCallCount as int 
    'etc. 
end class 

public class CallViewData 
    public AllCalls as CallInfo 
    public MyCalls as CallInfo 
    public UserName as String 
end class 

,并使用强类型的视图从ViewPage(of CallViewData)派生,这种方式你有智能感知,你不需要努力与硬编码的字符串来获取您的信息。

您使用来自所有调用和当前用户调用的信息填充CallViewData,并返回此实例。

Dim data as new CallViewData 
    data.AllCalls = new CallInfo {OpenCallCount = ... } 
    'etc 
    return View(data) 
0

你不必为你的ViewData分配数据项目。如果由于某种原因,您无法传递CallRepository对象,那么您可以创建另一个数据对象,它将保存您的开放和所有调用存储库数据并传递该对象。

这是一个设计,从长远来看实际上更加灵活。

相关问题