当前位置:  开发笔记 > 编程语言 > 正文

使用ASP.NET MVC将多个视图返回到一个ActionResult

如何解决《使用ASP.NETMVC将多个视图返回到一个ActionResult》经验,为你挑选了1个好方法。

我想要实现的目标是:

Items assigned to me

Item 1 assigned to me
Item 2 assigned to me
Item 3 assigned to me


All open items
Item 1 open to everyone
Item 2 open to everyone
Item 3 open to everyone
Item 4 open to everyone

虽然从目前为止我对MVC的经验是,我必须将数据返回到视图模型,以便能够以下列方式在视图中使用它:


    
Calls assigned to you (<%=Html.ViewData("MyOpenCallsCount")%>)
  Subject Logged Updated
   Assignee: <%=Html.ViewData("UserName")%>
<% For Each aT In ViewData.Model%> <% Next%>
  <%=Html.ActionLink(aT.Title, "Details", New With {.id = aT.CallID})%> <%=aT.loggedOn.Date.ToShortDateString%> <%=aT.updatedOn.Date.ToShortDateString%>

All unsolved calls (<%=Html.ViewData("OpenCallCount")%>)
  Subject Logged Priority Updated
<% For Each t As hdCall In ViewData.Model%> <% Next%>
<%=Html.ActionLink(t.Title, "Details", New With {.id = t.CallID})%> <%=t.loggedOn%> <%=t.hdPriority.Priority%> <%=t.updatedOn%>

现在,我有它的想法,即使我知道它不会工作,基本上是:

'
' GET: /Calls/
 _
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)
    Return View(viewMyOpenCalls)
End Function

我想知道的是,这样做的正确方法是什么?我不知道如何采用正确的方式,我认为我至少在那里有理论,而不是如何实现它.

在此先感谢您的帮助.


编辑

根据以下评论,我做了以下代码编辑/补充:

Class Calls
    Private _OpenCalls As hdCall
    Public Property OpenCalls() As hdCall
        Get
            Return _OpenCalls
        End Get
        Set(ByVal value As hdCall)
            _OpenCalls = value
        End Set
    End Property
    Private _MyCalls As hdCall
    Public Property MyCalls() As hdCall
        Get
            Return _MyCalls
        End Get
        Set(ByVal value As hdCall)
            _MyCalls = value
        End Set
    End Property
End Class

Index()动作

'
' GET: /Calls/
 _
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 As New Calls With {.OpenCalls = callRepository.FindAllOpenCalls()}
    Dim viewMyOpenCalls As New Calls With {.MyCalls = callRepository.FindAllMyCalls(Session("LoggedInUser"))}

    Return View(New Calls())
End Function

电话/的Index.aspx

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

<%@ Import Namespace="CustomerServiceHelpdesk" %>


    Topps Customer Service Helpdesk - View All Calls



    
Calls assigned to you (<%=Html.ViewData("MyOpenCallsCount")%>)
  Subject Logged Updated
   Assignee: <%=Html.ViewData("UserName")%>
<% For Each aT In Model.MyCalls%> <% Next%>
  <%=Html.ActionLink(aT.Title, "Details", New With {.id = aT.CallID})%> <%=aT.loggedOn.Date.ToShortDateString%> <%=aT.updatedOn.Date.ToShortDateString%>

All unsolved calls (<%=Html.ViewData("OpenCallCount")%>)
  Subject Logged Priority Updated
<% For Each t As hdCall In Model.OpenCalls%> <% Next%>
<%=Html.ActionLink(t.Title, "Details", New With {.id = t.CallID})%> <%=t.loggedOn%> <%=t.hdPriority.Priority%> <%=t.updatedOn%>

但是,该行<%=Html.ViewData("MyOpenCallsCount")%>给了我一个End of Statement预期错误.

此外,我确实得到它编译一次,但我得到了Unable to cast object of type 'System.Data.Linq.DataQuery1[CustomerServiceHelpdesk.hdCall]' to type 'CustomerServiceHelpdesk.hdCall'.该行的错误Dim viewOpenCalls As New Calls With {.OpenCalls = callRepository.FindAllOpenCalls()}

我哪里做错了?

当涉及到这样的事情时,我有点像菜鸟,所以任何帮助都非常感激.



1> Çağdaş Tekin..:

好吧,你不能从一个函数返回两个值,如果这是你想要做的(标题表明你想要的).
另外,这并不是如何http运作的.有永远只是一个为响应一个请求.

但是如果你试图将你的对象viewOpenCallsviewMyOpenCalls对象都发送到一个视图,那么你可以通过让你的视图拥有一个既能容纳它们的模型又可以做到这一点.

像这样 :

//My VB is a bit rusty so I'm writing this in C#
class Calls {
    public yourDataType OpenCalls { get; set; }
    public yourDataType MyCalls { get; set; }
}

在您的控制器操作中:

return View(new Calls { OpenCalls = viewOpenCalls,  MyCalls = viewMyOpenCalls })

//I gues in VB it would be like this :
Dim viewOpenCalls = callRepository.FindAllOpenCalls()
Dim viewMyOpenCalls = callRepository.FindAllMyCalls(Session("LoggedInUser"))
Return View(New Calls _
    With {.OpenCalls = viewOpenCalls, .MyCalls = viewMyOpenCalls})

在您的视图中,确保它的模型是类的Calls类型.

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

现在,您可以使用<%=Model.OpenCalls %>和访问属性<%=Model.MyCalls %>

推荐阅读
mobiledu2402851173
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有