从列表中获取对象<元组< object1,object2> >并存储在ViewModel中

时间:2010-11-15 23:03:16

标签: asp.net-mvc-2

[建议:想以合乎逻辑的方式阅读答案吗? >>选择TAB [最旧]

Goal: 
Presentation of books with related inventorydetails on homepage 
such as Book.Title, InventoryDetail.Quantity etc. 
(Join|Book.BookId <=< InventoryDetail.BookId)
  

问题1:如何加入

     

问题2:如何使用元组(元组列表)

     

问题3:如何将分离的对象(从元组列表中)存储到强类型的ViewModel中

     

答案1:使用Mike Brind指南的可行方法

     

答案2:解决问题1和2 !!

     

答案3:解决了问题3 !!

玩得开心。我很高兴分享!!!

public ActionResult Index()
{     
 // Return a list of tuples {(WebshopDB.Models.Book, WebshopDB.Models.InventoryDetail)}
 // Each tuple containing two items: 
 // > Item1 {WebshopDB.Models.Book}
 // > Item2 {WebshopDB.Models.InventoryDetail}
 var tuple_booksinventorydetails = ListOfTuples_BookInventoryDetail(5);

 // BEGIN UNDER CONSTRUCTION Setting up ViewModel
 // See below the code for the ViewModel
  var viewmodel = new HomeIndexViewModel()
  {
   // Problem // Book = tuple_books.Contains(Book).??,
   // Problem // InventoryDetail = tuple_books.Contains(InventoryDetail).??
  };
 // END

  return View(  .....  );
}

private List<Tuple<Book, InventoryDetail>> ListOfTuples_BookInventoryDetail(int count)
{
  var list_of_tuples = new List<Tuple<Book, InventoryDetail>>(); 

  var showbooks = webshopDB.Books
     .Join(webshopDB.InventoryDetails, b => b.BookId, i => i.BookId, (b, i) => new { b = b, i = i })
     .Where(o => (o.b.ShowInWebshop == true))
     .Where(o => o.b.BookThumbUrl.Contains(".jpg"))
     .Take(count);

  foreach (var item in showbooks)
  {
    list_of_tuples.Add( Tuple.Create<Book, InventoryDetail>( (item.b), (item.i) ) );
  }
  return list_of_tuples;
}

2 个答案:

答案 0 :(得分:0)

答案(解决我自己发布的问题!)

  // Instantiate new lists needed to add the to be separated objects of the tuple
  List<Book> T1 = new List<Book>();
  List<InventoryDetail> T2 = new List<InventoryDetail>();

  // Iterate through the tuple and add each 'item' of each tuple into the new instantiated lists
  for (int i = 0; i < tuple_booksinventorydetails.Count; i++)
  {
    var tuple = tuple_booksinventorydetails[i];
    // Item1
    T1.Add(tuple.Item1);
    // Item2
    T2.Add(tuple.Item2);
  }

  // Instantiate a new viewmodel to store the separated lists

  // ==HomeTupleIndexViewMode Class==
  //using System;
  //using System.Collections.Generic;
  //using <Domain>.Models;
  //namespace <Domain>.ViewModels
  //{
  //  public class HomeTupleIndexViewModel
  //  {
  //    public List<Book> Book { get; set; }
  //    public List<InventoryDetail> InventoryDetail { get; set; }
  //  }
  //}
  // ==

  HomeTupleIndexViewModel tupleviewdata = new HomeTupleIndexViewModel() 
  { 
   Book = T1,
   InventoryDetail = T2
  };
  return View(tupleviewdata);

答案 1 :(得分:0)

如同承诺的那样!我确实达到了目标!!!

<%-- Index.apsx --%>
<%@ Page Title=" <Domain> " Language="C#" MasterPageFile="~/Views/Shared/Store.Master"
    Inherits="System.Web.Mvc.ViewPage<HomeTupleIndexViewModel>" %>

<%-- Mandatory 'Import' --%>
<%@ Import Namespace="<Domain>.ViewModels" %>   

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
  <%--....--%>
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
  <%-- Exerecise Tuple --%>
  <% Html.RenderPartial("BookPartial", Model.Book); %>
  <% Html.RenderPartial("InventoryDetailPartial", Model.InventoryDetail); %>
</asp:Content>


<%-- BookPartial.ascx --%>
<%-- Currently this ViewPartial is a Strongly Typed ViewPartial %> --%>
<%@ Control Language="C#" 
    Inherits="System.Web.Mvc.ViewUserControl<IEnumerable< <Domain> .Models.Book>>" %>
<%@ Import Namespace=" <Domain> .Helpers" %>
<%-- Strongly Typed ViewPages--%>
<ul id="...">
  <% foreach (var item in Model) {%>  
<li>
  <div id="...">
    <p>
      <a href="<%: Url.Action("Details", "Store", new { id = item.BookId }) %>">
        <div id="...">
          <h2>
            <span><%: item.Genre %></span>
          </h2>
        </div>
        <div id="...">
          <img alt="<%: item.Title %>" src="<%: item.BookThumbUrl %>" />
        </div>
        <div id="...">  
          <span><%: item.Title %></span>
        </div>
      </a> 
    </p>
  </div>
    </li> 
  <% } %>
</ul>


<%-- InventoryDetailPartial.ascx --%>
<%@ Control Language="C#" 
    Inherits="System.Web.Mvc.ViewUserControl<IEnumerable< <Domain> .Models.InventoryDetail>>" %>
<%@ Import Namespace=" <Domain> .Helpers" %>
<%-- Strongly Typed ViewPages--%>
<ul id="...">
  <% foreach (var item in Model) {%>  
<li>
  <div id="...">
    <p> 
      <div id="...">  
        <span>
          <% if (item.Quantity == 1) 
                 { %> 
             <%: Server.HtmlDecode(Html.Translation("Stock_Amount"))%>&nbsp;<%: item.Quantity %>&nbsp;<%: Server.HtmlDecode(Html.Translation("Copy"))%>
          <% } 
                 else if (item.Quantity > 1) %> 
              <% { %>
             <%: Server.HtmlDecode(Html.Translation("Stock_Amount"))%>&nbsp;<%: item.Quantity %>&nbsp;<%: Server.HtmlDecode(Html.Translation("Copies"))%>
          <% } 
                 else 
                 { %>
             <%: Server.HtmlDecode(Html.Translation("Sold_Out"))%>
          <% } %>
        </span>
      </div>
    </p>
  </div>
</li>
  <% } %>
</ul>

所以,最后我最终得到一个Index.aspx调用两个单独的部分视图(我更喜欢谈论'控制'而不是'部分',但这只是个人品味的问题。)

与Mike Brind的指南(在我的第一个答案中提到)相比,通过使用这个元组“路径”指南,您最终可以在索引页面上获得更多“自由”。 '相关'对象(!!'加'记得!!)在表示层中很好地分开,但仍然有很强的相关性!