2009-10-21 7 views
2

Ce que je veux atteindre est essentiellement:plusieurs vues à un ActionResult avec ASP.NET MVC

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 

Bien que ce que je l'ai connu de MVC à ce jour est que je dois retourner les données à la modèle en vue de pouvoir l'utiliser dans la vue elle-même de la manière suivante:

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

maintenant, l'idée que j'ai pour elle, même si je sais que cela ne fonctionnera pas serait essentiellement:

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

Ce que je me demande, c'est quelle serait la bonne façon de procéder? Je n'ai pas la moindre idée de comment aller dans le bon sens, je pense avoir au moins la théorie là-bas, mais pas comment la mettre en œuvre.

Merci pour toute aide à l'avance.


EDIT

Sur la base des commentaires ci-dessous, j'ai fait le code suivant modifie/ajouts:

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 

Appels/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> 

Cependant, la ligne <%=Html.ViewData("MyOpenCallsCount")%> me donne une fin de déclaration d'erreur attendue.

Aussi, je l'ai fait pour le compiler une fois, mais je suis l'erreur Unable to cast object of type 'System.Data.Linq.DataQuery1[CustomerServiceHelpdesk.hdCall]' to type 'CustomerServiceHelpdesk.hdCall'. de la ligne Dim viewOpenCalls As New Calls With {.OpenCalls = callRepository.FindAllOpenCalls()}

Où est-ce que je me trompe?

Je suis un peu noob quand il s'agit de choses comme ça, donc toute aide est très appréciée.

+0

@Liam, au sujet de votre edit: j'ai changé le deuxième exemple de code pour vous montrer comment il est fait en VB. Vérifie ma réponse –

+0

Avec cette modification, j'obtiens l'erreur: Impossible de lancer l'objet de type 'System.Data.Linq.DataQuery'1 [CustomerServiceHelpdesk.hdCall]' pour taper 'CustomerServiceHelpdesk.hdCall'. Mais, je pense que j'utilise le bon type de données de hdCall (ma table d'appels). – LiamGu

Répondre

2

Une fois que j'ai trouvé la bonne façon de le faire (un peu de temps maintenant), je l'ai fait fonctionner.

Pour référence, c'est la façon dont il devrait être fait:

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 
10

Eh bien, vous ne pouvez pas retourner deux valeurs d'une fonction, si c'est ce que vous essayez de faire (le titre suggère que c'est ce que vous voulez).
De plus, ce n'est pas comme ça que fonctionne http. Il ya toujours une réponse pour une demande. Mais si vous essayez d'envoyer vos objets viewOpenCalls et viewMyOpenCalls à une même vue, vous pouvez faire en sorte que votre vue contienne un modèle qui les contienne tous les deux.

Comme ceci:

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

Dans votre action du contrôleur:

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

Selon vous assurez-vous qu'il est le modèle type de la classe Calls.

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

Et vous pouvez maintenant accéder aux propriétés avec <%=Model.OpenCalls %> et <%=Model.MyCalls %>

+0

Que dois-je rechercher pour savoir comment faire cela? – LiamGu

+0

@Liam, j'ai édité la réponse. –

+0

Ceci est assez courant, en particulier lorsque vous renvoyez plusieurs jeux de résultats à partir de procédures stockées. Je fais ceci dans mon application MVC - vous écrivez juste des conteneurs pour plusieurs ensembles de données pour retourner à la vue. – Kezzer

1

1) Créer une classe ViewData. Ceci est juste une classe POCO avec des propriétés définies pour chaque élément de données que vous voulez afficher sur la vue (par exemple OpenCallCount)

2) Créez une vue fortement typée qui utilise cette classe ViewData.

3) Passez une nouvelle instance de votre classe ViewData, avec le jeu de propriétés, à votre vue.

Cela vous aidera à éviter d'utiliser des chaînes magiques partout (par exemple ViewData (« OpenCallCount ») = ... devient myViewDataClass.OpenCallCount = ...)

Vous pourriez probablement ranger la vue en utilisant deux vue partielle classes, ou le rendant légèrement plus générique, mais il fera le travail en ce moment.