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

发布于 2024-08-08 08:26:48 字数 16421 浏览 1 评论 0原文

我想要实现的本质是:

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 的体验来看,我必须将数据返回到视图模型,以便能够通过以下方式在视图本身中使用它:

<asp:Content ID="ticketsContent" ContentPlaceHolderID="MainContent" runat="server">
    <div id="hdMain">
        <div id="hdMainTop"><img src="images/hdMainTop.gif" alt="" /></div>
        <div id="hdMainContent">
            <div id="numberOfCalls">Calls assigned to you (<%=Html.ViewData("MyOpenCallsCount")%>)</div>
            <div id="assignedToMe">
                <div id="callHeaders">
                    <table id="callHeadersTbl" cellpadding="0" cellspacing="0">
                        <tr>
                            <td width="54">&nbsp;</td>
                            <td width="270">Subject</td>
                            <td width="148">Logged</td>
                            <td width="120">Updated</td>
                        </tr>
                    </table>
                </div>
                <div id="greyTicketBar">&nbsp;&nbsp; Assignee: <strong><%=Html.ViewData("UserName")%></strong></div>
                <table cellpadding="0" cellspacing="0" width="643">
                        <% For Each aT In ViewData.Model%>
                            <tr>
                                <td width="54" class="ticketList">&nbsp;</td>
                                <td width="270" class="ticketList"><%=Html.ActionLink(aT.Title, "Details", New With {.id = aT.CallID})%></td>
                                <td width="148" class="ticketList"><%=aT.loggedOn.Date.ToShortDateString%></td>
                                <td width="115" class="ticketList"><%=aT.updatedOn.Date.ToShortDateString%></td>
                            </tr>
                            <% Next%>
                        </table>
            </div>
        </div>
        <div id="hdMainBottom"><img src="images/hdMainBottom.gif" alt="" /></div>
        <div id="bigbreak">
            <br />
        </div>
        <div id="hdMainTop"><img src="images/hdMainTop.gif" alt="" /></div>
        <div id="hdMainContent">
            <div id="numberOfCalls">All unsolved calls (<%=Html.ViewData("OpenCallCount")%>)</div>
            <div id="unsolvedTix">
                <div id="callHeaders">
                        <table id="callHeadersTbl" cellpadding="0" cellspacing="0">
                            <tr>
                                <td width="54">&nbsp;</td>
                                <td width="270">Subject</td>
                                <td width="148">Logged</td>
                                <td width="58">Priority</td>
                                <td width="120">Updated</td>
                            </tr>
                        </table>
                    </div>
                    <div id="greyTicketBar"></div>
                    <table cellpadding="0" cellspacing="0" width="643">
                        <% For Each t As hdCall In ViewData.Model%>
                            <tr>
                                <td width="51" class="ticketList" align="center"><img src="/images/icons/<%=t.hdPriority.Priority%>.gif" /></td>
                                <td width="270" class="ticketList"><%=Html.ActionLink(t.Title, "Details", New With {.id = t.CallID})%></td>
                                <td width="148" class="ticketList"><%=t.loggedOn%></td>
                                <td width="58" class="ticketList"><%=t.hdPriority.Priority%></td>
                                <td width="115" class="ticketList"><%=t.updatedOn%></td>
                            </tr>
                            <% Next%>
                        </table>
                </div>
        </div>
        <div id="hdMainBottom"><img src="images/hdMainBottom.gif" alt="" /></div>
    </div>
    <div id="hdSpacer"></div>
    <div id="hdMenus">
        <div id="browseBox">
            <div id="blueTop"><img src="images/blueboxTop.gif" /></div>
            <div id="blueContent">
                <img src="images/browse.gif" alt="Browse" /><br /><br />
                <ul>
                    <li>&nbsp;<a href="/Calls/Company/1">Calls for Topps</a><br /><br /></li>
                    <li>&nbsp;<a href="/Calls/Company/2">Calls for TCH</a><br /><br /></li>
                </ul>
            </div>
            <div id="blueBottom"><img src="images/blueboxBottom.gif" /></div>
            <br />
             <div id="Dashboard">
            <div id="blueTop"><img src="images/blueboxTop.gif" /></div>
            <div id="blueContent"><img src="images/dashboard.gif" alt="Dashboard" /><br /><br />
                <div id="storePercent"><%=Html.ViewData("OpenCallCount")%><br />
                    Calls Open</div>
                <ul style="font-weight: bold;">
                    <li>&nbsp;<a href="/Calls/Urgent">Urgent:&nbsp;<%=Html.ViewData("UrgentCallCount")%></a><br /><br /></li>
                    <li>&nbsp;<a href="/Calls/High">High:&nbsp;<%=Html.ViewData("HighCallCount")%></a><br /><br /></li>
                    <li>&nbsp;<a href="/Calls/Normal">Normal:&nbsp;<%=Html.ViewData("NormalCallCount")%></a><br /><br /></li>
                    <li>&nbsp;<a href="/Calls/Low">Low:&nbsp;<%=Html.ViewData("LowCallCount")%></a></li>
                </ul>
            </div>
            <div id="blueBottom"><img src="images/blueboxBottom.gif" /></div>
        </div>
        </div>
</asp:Content>

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

'
' 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)
    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() Action

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

    Return View(New Calls())
End Function

Calls/Index.aspx

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

<%@ Import Namespace="CustomerServiceHelpdesk" %>

<asp:Content ID="ticketsHeader" ContentPlaceHolderID="TitleContent" runat="server">
    Topps Customer Service Helpdesk - View All Calls
</asp:Content>

<asp:Content ID="ticketsContent" ContentPlaceHolderID="MainContent" runat="server">
    <div id="hdMain">
        <div id="hdMainTop"><img src="images/hdMainTop.gif" alt="" /></div>
        <div id="hdMainContent">
            <div id="numberOfCalls">Calls assigned to you (<%=Html.ViewData("MyOpenCallsCount")%>)</div>
            <div id="assignedToMe">
                <div id="callHeaders">
                    <table id="callHeadersTbl" cellpadding="0" cellspacing="0">
                        <tr>
                            <td width="54">&nbsp;</td>
                            <td width="270">Subject</td>
                            <td width="148">Logged</td>
                            <td width="120">Updated</td>
                        </tr>
                    </table>
                </div>
                <div id="greyTicketBar">&nbsp;&nbsp; Assignee: <strong><%=Html.ViewData("UserName")%></strong></div>
                <table cellpadding="0" cellspacing="0" width="643">
                        <% For Each aT In Model.MyCalls%>
                            <tr>
                                <td width="54" class="ticketList">&nbsp;</td>
                                <td width="270" class="ticketList"><%=Html.ActionLink(aT.Title, "Details", New With {.id = aT.CallID})%></td>
                                <td width="148" class="ticketList"><%=aT.loggedOn.Date.ToShortDateString%></td>
                                <td width="115" class="ticketList"><%=aT.updatedOn.Date.ToShortDateString%></td>
                            </tr>
                            <% Next%>
                        </table>
            </div>
        </div>
        <div id="hdMainBottom"><img src="images/hdMainBottom.gif" alt="" /></div>
        <div id="bigbreak">
            <br />
        </div>
        <div id="hdMainTop"><img src="images/hdMainTop.gif" alt="" /></div>
        <div id="hdMainContent">
            <div id="numberOfCalls">All unsolved calls (<%=Html.ViewData("OpenCallCount")%>)</div>
            <div id="unsolvedTix">
                <div id="callHeaders">
                        <table id="callHeadersTbl" cellpadding="0" cellspacing="0">
                            <tr>
                                <td width="54">&nbsp;</td>
                                <td width="270">Subject</td>
                                <td width="148">Logged</td>
                                <td width="58">Priority</td>
                                <td width="120">Updated</td>
                            </tr>
                        </table>
                    </div>
                    <div id="greyTicketBar"></div>
                    <table cellpadding="0" cellspacing="0" width="643">
                        <% For Each t As hdCall In Model.OpenCalls%>
                            <tr>
                                <td width="51" class="ticketList" align="center"><img src="/images/icons/<%=t.hdPriority.Priority%>.gif" /></td>
                                <td width="270" class="ticketList"><%=Html.ActionLink(t.Title, "Details", New With {.id = t.CallID})%></td>
                                <td width="148" class="ticketList"><%=t.loggedOn%></td>
                                <td width="58" class="ticketList"><%=t.hdPriority.Priority%></td>
                                <td width="115" class="ticketList"><%=t.updatedOn%></td>
                            </tr>
                            <% Next%>
                        </table>
                </div>
        </div>
        <div id="hdMainBottom"><img src="images/hdMainBottom.gif" alt="" /></div>
    </div>
    <div id="hdSpacer"></div>
    <div id="hdMenus">
        <div id="browseBox">
            <div id="blueTop"><img src="images/blueboxTop.gif" /></div>
            <div id="blueContent">
                <img src="images/browse.gif" alt="Browse" /><br /><br />
                <ul>
                    <li>&nbsp;<a href="/Calls/Company/1">Calls for Topps</a><br /><br /></li>
                    <li>&nbsp;<a href="/Calls/Company/2">Calls for TCH</a><br /><br /></li>
                </ul>
            </div>
            <div id="blueBottom"><img src="images/blueboxBottom.gif" /></div>
            <br />
             <div id="Dashboard">
            <div id="blueTop"><img src="images/blueboxTop.gif" /></div>
            <div id="blueContent"><img src="images/dashboard.gif" alt="Dashboard" /><br /><br />
                <div id="storePercent"><%=Html.ViewData("OpenCallCount")%><br />
                    Calls Open</div>
                <ul style="font-weight: bold;">
                    <li>&nbsp;<a href="/Calls/Urgent">Urgent:&nbsp;<%=Html.ViewData("UrgentCallCount")%></a><br /><br /></li>
                    <li>&nbsp;<a href="/Calls/High">High:&nbsp;<%=Html.ViewData("HighCallCount")%></a><br /><br /></li>
                    <li>&nbsp;<a href="/Calls/Normal">Normal:&nbsp;<%=Html.ViewData("NormalCallCount")%></a><br /><br /></li>
                    <li>&nbsp;<a href="/Calls/Low">Low:&nbsp;<%=Html.ViewData("LowCallCount")%></a></li>
                </ul>
            </div>
            <div id="blueBottom"><img src="images/blueboxBottom.gif" /></div>
        </div>
        </div>
</asp:Content>

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

另外,我确实编译了一次,但出现错误 Unable tocast object of type 'System.Data.Linq.DataQuery1[CustomerServiceHelpdesk.hdCall]' to type 'CustomerServiceHelpdesk.hdCall'。从行 Dim viewOpenCalls As New Calls With {.OpenCalls = callRepository.FindAllOpenCalls()}

我哪里出错了?

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

What I want to achieve essentially is:

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

Though from what I have experienced of MVC so far is that I would have to return the data to the view model to be able to use it in the view itself in the following manner:

<asp:Content ID="ticketsContent" ContentPlaceHolderID="MainContent" runat="server">
    <div id="hdMain">
        <div id="hdMainTop"><img src="images/hdMainTop.gif" alt="" /></div>
        <div id="hdMainContent">
            <div id="numberOfCalls">Calls assigned to you (<%=Html.ViewData("MyOpenCallsCount")%>)</div>
            <div id="assignedToMe">
                <div id="callHeaders">
                    <table id="callHeadersTbl" cellpadding="0" cellspacing="0">
                        <tr>
                            <td width="54"> </td>
                            <td width="270">Subject</td>
                            <td width="148">Logged</td>
                            <td width="120">Updated</td>
                        </tr>
                    </table>
                </div>
                <div id="greyTicketBar">   Assignee: <strong><%=Html.ViewData("UserName")%></strong></div>
                <table cellpadding="0" cellspacing="0" width="643">
                        <% For Each aT In ViewData.Model%>
                            <tr>
                                <td width="54" class="ticketList"> </td>
                                <td width="270" class="ticketList"><%=Html.ActionLink(aT.Title, "Details", New With {.id = aT.CallID})%></td>
                                <td width="148" class="ticketList"><%=aT.loggedOn.Date.ToShortDateString%></td>
                                <td width="115" class="ticketList"><%=aT.updatedOn.Date.ToShortDateString%></td>
                            </tr>
                            <% Next%>
                        </table>
            </div>
        </div>
        <div id="hdMainBottom"><img src="images/hdMainBottom.gif" alt="" /></div>
        <div id="bigbreak">
            <br />
        </div>
        <div id="hdMainTop"><img src="images/hdMainTop.gif" alt="" /></div>
        <div id="hdMainContent">
            <div id="numberOfCalls">All unsolved calls (<%=Html.ViewData("OpenCallCount")%>)</div>
            <div id="unsolvedTix">
                <div id="callHeaders">
                        <table id="callHeadersTbl" cellpadding="0" cellspacing="0">
                            <tr>
                                <td width="54"> </td>
                                <td width="270">Subject</td>
                                <td width="148">Logged</td>
                                <td width="58">Priority</td>
                                <td width="120">Updated</td>
                            </tr>
                        </table>
                    </div>
                    <div id="greyTicketBar"></div>
                    <table cellpadding="0" cellspacing="0" width="643">
                        <% For Each t As hdCall In ViewData.Model%>
                            <tr>
                                <td width="51" class="ticketList" align="center"><img src="/images/icons/<%=t.hdPriority.Priority%>.gif" /></td>
                                <td width="270" class="ticketList"><%=Html.ActionLink(t.Title, "Details", New With {.id = t.CallID})%></td>
                                <td width="148" class="ticketList"><%=t.loggedOn%></td>
                                <td width="58" class="ticketList"><%=t.hdPriority.Priority%></td>
                                <td width="115" class="ticketList"><%=t.updatedOn%></td>
                            </tr>
                            <% Next%>
                        </table>
                </div>
        </div>
        <div id="hdMainBottom"><img src="images/hdMainBottom.gif" alt="" /></div>
    </div>
    <div id="hdSpacer"></div>
    <div id="hdMenus">
        <div id="browseBox">
            <div id="blueTop"><img src="images/blueboxTop.gif" /></div>
            <div id="blueContent">
                <img src="images/browse.gif" alt="Browse" /><br /><br />
                <ul>
                    <li> <a href="/Calls/Company/1">Calls for Topps</a><br /><br /></li>
                    <li> <a href="/Calls/Company/2">Calls for TCH</a><br /><br /></li>
                </ul>
            </div>
            <div id="blueBottom"><img src="images/blueboxBottom.gif" /></div>
            <br />
             <div id="Dashboard">
            <div id="blueTop"><img src="images/blueboxTop.gif" /></div>
            <div id="blueContent"><img src="images/dashboard.gif" alt="Dashboard" /><br /><br />
                <div id="storePercent"><%=Html.ViewData("OpenCallCount")%><br />
                    Calls Open</div>
                <ul style="font-weight: bold;">
                    <li> <a href="/Calls/Urgent">Urgent: <%=Html.ViewData("UrgentCallCount")%></a><br /><br /></li>
                    <li> <a href="/Calls/High">High: <%=Html.ViewData("HighCallCount")%></a><br /><br /></li>
                    <li> <a href="/Calls/Normal">Normal: <%=Html.ViewData("NormalCallCount")%></a><br /><br /></li>
                    <li> <a href="/Calls/Low">Low: <%=Html.ViewData("LowCallCount")%></a></li>
                </ul>
            </div>
            <div id="blueBottom"><img src="images/blueboxBottom.gif" /></div>
        </div>
        </div>
</asp:Content>

Now, the idea I have for it, even though I know it won't work would basically be:

'
' 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)
    Return View(viewMyOpenCalls)
End Function

What I'm wondering is, what would be the correct way to do this? I haven't a clue as how to go about the right way, I think I at least have the theory there though, just not how to implement it.

Thanks for any help in advance.


EDIT

Based on the below comments, I have made the following code edits/additions:

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() Action

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

    Return View(New Calls())
End Function

Calls/Index.aspx

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

<%@ Import Namespace="CustomerServiceHelpdesk" %>

<asp:Content ID="ticketsHeader" ContentPlaceHolderID="TitleContent" runat="server">
    Topps Customer Service Helpdesk - View All Calls
</asp:Content>

<asp:Content ID="ticketsContent" ContentPlaceHolderID="MainContent" runat="server">
    <div id="hdMain">
        <div id="hdMainTop"><img src="images/hdMainTop.gif" alt="" /></div>
        <div id="hdMainContent">
            <div id="numberOfCalls">Calls assigned to you (<%=Html.ViewData("MyOpenCallsCount")%>)</div>
            <div id="assignedToMe">
                <div id="callHeaders">
                    <table id="callHeadersTbl" cellpadding="0" cellspacing="0">
                        <tr>
                            <td width="54"> </td>
                            <td width="270">Subject</td>
                            <td width="148">Logged</td>
                            <td width="120">Updated</td>
                        </tr>
                    </table>
                </div>
                <div id="greyTicketBar">   Assignee: <strong><%=Html.ViewData("UserName")%></strong></div>
                <table cellpadding="0" cellspacing="0" width="643">
                        <% For Each aT In Model.MyCalls%>
                            <tr>
                                <td width="54" class="ticketList"> </td>
                                <td width="270" class="ticketList"><%=Html.ActionLink(aT.Title, "Details", New With {.id = aT.CallID})%></td>
                                <td width="148" class="ticketList"><%=aT.loggedOn.Date.ToShortDateString%></td>
                                <td width="115" class="ticketList"><%=aT.updatedOn.Date.ToShortDateString%></td>
                            </tr>
                            <% Next%>
                        </table>
            </div>
        </div>
        <div id="hdMainBottom"><img src="images/hdMainBottom.gif" alt="" /></div>
        <div id="bigbreak">
            <br />
        </div>
        <div id="hdMainTop"><img src="images/hdMainTop.gif" alt="" /></div>
        <div id="hdMainContent">
            <div id="numberOfCalls">All unsolved calls (<%=Html.ViewData("OpenCallCount")%>)</div>
            <div id="unsolvedTix">
                <div id="callHeaders">
                        <table id="callHeadersTbl" cellpadding="0" cellspacing="0">
                            <tr>
                                <td width="54"> </td>
                                <td width="270">Subject</td>
                                <td width="148">Logged</td>
                                <td width="58">Priority</td>
                                <td width="120">Updated</td>
                            </tr>
                        </table>
                    </div>
                    <div id="greyTicketBar"></div>
                    <table cellpadding="0" cellspacing="0" width="643">
                        <% For Each t As hdCall In Model.OpenCalls%>
                            <tr>
                                <td width="51" class="ticketList" align="center"><img src="/images/icons/<%=t.hdPriority.Priority%>.gif" /></td>
                                <td width="270" class="ticketList"><%=Html.ActionLink(t.Title, "Details", New With {.id = t.CallID})%></td>
                                <td width="148" class="ticketList"><%=t.loggedOn%></td>
                                <td width="58" class="ticketList"><%=t.hdPriority.Priority%></td>
                                <td width="115" class="ticketList"><%=t.updatedOn%></td>
                            </tr>
                            <% Next%>
                        </table>
                </div>
        </div>
        <div id="hdMainBottom"><img src="images/hdMainBottom.gif" alt="" /></div>
    </div>
    <div id="hdSpacer"></div>
    <div id="hdMenus">
        <div id="browseBox">
            <div id="blueTop"><img src="images/blueboxTop.gif" /></div>
            <div id="blueContent">
                <img src="images/browse.gif" alt="Browse" /><br /><br />
                <ul>
                    <li> <a href="/Calls/Company/1">Calls for Topps</a><br /><br /></li>
                    <li> <a href="/Calls/Company/2">Calls for TCH</a><br /><br /></li>
                </ul>
            </div>
            <div id="blueBottom"><img src="images/blueboxBottom.gif" /></div>
            <br />
             <div id="Dashboard">
            <div id="blueTop"><img src="images/blueboxTop.gif" /></div>
            <div id="blueContent"><img src="images/dashboard.gif" alt="Dashboard" /><br /><br />
                <div id="storePercent"><%=Html.ViewData("OpenCallCount")%><br />
                    Calls Open</div>
                <ul style="font-weight: bold;">
                    <li> <a href="/Calls/Urgent">Urgent: <%=Html.ViewData("UrgentCallCount")%></a><br /><br /></li>
                    <li> <a href="/Calls/High">High: <%=Html.ViewData("HighCallCount")%></a><br /><br /></li>
                    <li> <a href="/Calls/Normal">Normal: <%=Html.ViewData("NormalCallCount")%></a><br /><br /></li>
                    <li> <a href="/Calls/Low">Low: <%=Html.ViewData("LowCallCount")%></a></li>
                </ul>
            </div>
            <div id="blueBottom"><img src="images/blueboxBottom.gif" /></div>
        </div>
        </div>
</asp:Content>

However, the line <%=Html.ViewData("MyOpenCallsCount")%> gives me an End of Statement expected error.

Also, I did get it to compile once, but I got the error Unable to cast object of type 'System.Data.Linq.DataQuery1[CustomerServiceHelpdesk.hdCall]' to type 'CustomerServiceHelpdesk.hdCall'. from the line Dim viewOpenCalls As New Calls With {.OpenCalls = callRepository.FindAllOpenCalls()}

Where did I go wrong?

I'm a bit of noob when it comes to things like this, so any help is much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

自此以后,行同陌路 2024-08-15 08:26:48

好吧,如果您想要执行此操作,则不能从函数返回两个值(标题表明这就是您想要的)。
另外,这并不是 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<Calls>" %>

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

Well, you can't return two values from a function, if that's what you are trying to do (the title suggests that's what you want).
Plus that's not how http works anyway. There's always just one response for a request.

But if you are trying to send both your viewOpenCalls and viewMyOpenCalls objects to one view, then you can do that with making your view have a model that'll hold both of them.

Like this :

//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; }
}

In your controller action :

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})

In your view make sure it's model is type of the Calls class.

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

And now you can access the properties with <%=Model.OpenCalls %> and <%=Model.MyCalls %>

山有枢 2024-08-15 08:26:48

一旦我找到了正确的方法(现在就开始),我就开始工作了。

作为参考,应该这样做:

Public Class TheCalls
Private _OpenCalls As IQueryable(Of hdCall)
Public Property OpenCalls() As IQueryable(Of hdCall)
    Get
        Return _OpenCalls
    End Get
    Set(ByVal value As IQueryable(Of hdCall))
        _OpenCalls = value
    End Set
End Property
Private _AssignedCalls As IQueryable(Of hdCall)
Public Property AssignedCalls() As IQueryable(Of hdCall)
    Get
        Return _AssignedCalls
    End Get
    Set(ByVal value As IQueryable(Of hdCall))
        _AssignedCalls = value
    End Set
End Property
End Class

Once I'd worked out the correct way of doing it (a little while a go now) I got it working.

For reference, this is the way it should be done:

Public Class TheCalls
Private _OpenCalls As IQueryable(Of hdCall)
Public Property OpenCalls() As IQueryable(Of hdCall)
    Get
        Return _OpenCalls
    End Get
    Set(ByVal value As IQueryable(Of hdCall))
        _OpenCalls = value
    End Set
End Property
Private _AssignedCalls As IQueryable(Of hdCall)
Public Property AssignedCalls() As IQueryable(Of hdCall)
    Get
        Return _AssignedCalls
    End Get
    Set(ByVal value As IQueryable(Of hdCall))
        _AssignedCalls = value
    End Set
End Property
End Class
双手揣兜 2024-08-15 08:26:48

1)创建一个ViewData类。这只是一个 POCO 类,其中为要在视图上显示的每个数据项定义了属性(例如 OpenCallCount)

2) 创建使用此 ViewData 类的强类型视图。

3) 将 ViewData 类的新实例(已设置属性)传递给您的视图。

这将帮助您避免在任何地方使用魔术字符串(例如 ViewData("OpenCallCount") = ... 变为 myViewDataClass.OpenCallCount = ...)

您可能可以通过使用两个部分视图类来整理视图,或者使其稍微更通用,但目前它可以完成这项工作。

1) Create a ViewData class. This is just a POCO class with properties defined for each data item you want to show on the view (e.g. OpenCallCount)

2) Create a strongly typed view that uses this ViewData class.

3) Pass a new instance of your ViewData class, with the properties set, to your view.

This will help you avoid using magic strings everywhere (e.g. ViewData("OpenCallCount") = ... becomes myViewDataClass.OpenCallCount = ...)

You could probably tidy up the view by using two partial view classes, or making it slightly more generic, but it will do the job at the moment.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文