asp.net mvc 2 - Get object out of List< Tuple < object1, object2 > > and store in ViewModel -


[suggestion: want read answers in logical manner ?? >> choose tab [oldest]

goal:  presentation of books related inventorydetails on homepage  such book.title, inventorydetail.quantity etc.  (join|book.bookid <=< inventorydetail.bookid) 

problem 1: how join

problem 2: how use tuple (list of tuples)

problem 3: how store separated objects (from list of tuples) typed viewmodel

answer 1: possible approach using mike brind's guidance

answer 2: problem 1 , 2 tackled !!

answer 3: problem 3 tackled !!

have fun it. i'm happy share!!!

public actionresult index() {       // return list of tuples {(webshopdb.models.book, webshopdb.models.inventorydetail)}  // each tuple containing 2 items:   // > item1 {webshopdb.models.book}  // > item2 {webshopdb.models.inventorydetail}  var tuple_booksinventorydetails = listoftuples_bookinventorydetail(5);   // begin under construction setting viewmodel  // see below code 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.bookid, (b, i) => new { b = b, = })      .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; } 

as promised! did reach goal!!!

<%-- 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 --%> <%-- viewpartial typed viewpartial %> --%> <%@ control language="c#"      inherits="system.web.mvc.viewusercontrol<ienumerable< <domain> .models.book>>" %> <%@ import namespace=" <domain> .helpers" %> <%-- 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" %> <%-- 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> 

so, ended index.aspx calling 2 separate partial views (i prefer speak 'controls' instead of 'partials', however, matter of personal taste).

compared mike brind's guide (mentioned in first answer) using tuple 'path' guide you'll have more 'freedom' on index page. 'related' objects (!! 'join' remember !!) nicely separated in presentation layer, still related!


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

html - Instapaper-like algorithm -

c# - How to execute a particular part of code asynchronously in a class -