gpt4 book ai didi

asp.net-mvc - 使用 ASP.NET MVC 将多个 View 返回到一个 ActionResult

转载 作者:行者123 更新时间:2023-12-02 05:46:22 24 4
gpt4 key购买 nike

本质上我想实现的是:

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 的体验是我必须将数据返回到 View 模型才能以下列方式在 View 本身中使用它:

<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() 操作

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

调用/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")%> 行给我一个语句结束预期错误。

此外,我确实让它编译了一次,但我得到了错误 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()}

我哪里出错了?

对于这样的事情我有点菜鸟,所以非常感谢任何帮助。

最佳答案

好吧,你不能从一个函数返回两个值,如果这是你想要做的(标题表明这就是你想要的)。
再加上这不是http无论如何工作。对于一个请求总是只有一个响应。

但是如果你想同时发送你的 viewOpenCallsviewMyOpenCalls对象到一个 View ,那么您可以通过让您的 View 有一个模型来容纳它们。

像这样:

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

在你的 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})

在您看来,确保它的模型是 Calls 的类型类。

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

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

关于asp.net-mvc - 使用 ASP.NET MVC 将多个 View 返回到一个 ActionResult,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1599821/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com