2010-05-28 16 views
7

में मॉडल बाध्यकारी नेस्टेड संग्रह मैं स्टीव सैंडर्सन के BeginCollectionItem helper का उपयोग एएसपी.नेट एमवीसी 2 के साथ कर रहा हूं ताकि वस्तुओं को संग्रहित करने के लिए मॉडल को बाध्य किया जा सके।एएसपी.नेट एमवीसी

यह ठीक काम करता है, जब तक संग्रह वस्तुओं के मॉडल में कोई अन्य संग्रह नहीं होता है।

मैं इस तरह एक मॉडल है: जब भी मैं प्रस्तुत करना और मॉडल प्रकार संग्रह बाँध

-उत्पाद
--Variants
--- IncludedAttributes

, यह jusst ठीक काम करता है। लेकिन IncludedAttributes संग्रह के साथ, मैं BeginCollectionItem सहायक क्योंकि आईडी और नाम मूल्य आईडी और नाम मूल्य है कि यह के लिए तैयार की गई थी सम्मान नहीं होगा माता पिता संस्करण है का उपयोग नहीं कर सकते हैं:

<div class="variant"> 
    <input type="hidden" value="bbd4fdd4-fa22-49f9-8a5e-3ff7e2942126" autocomplete="off" name="Variants.index"> 
    <input type="hidden" value="0" name="Variants[bbd4fdd4-fa22-49f9-8a5e-3ff7e2942126].SlotAmount" id="Variants_bbd4fdd4-fa22-49f9-8a5e-3ff7e2942126__SlotAmount"> 
    <table class="included-attributes"> 
     <input type="hidden" value="0" name="Variants.IncludedAttributes[c5989db5-b1e1-485b-b09d-a9e50dd1d2cb].Id" id="Variants_IncludedAttributes_c5989db5-b1e1-485b-b09d-a9e50dd1d2cb__Id" class="attribute-id"> 
     <tr> 
      <td> 
       <input type="hidden" value="0" name="Variants.IncludedAttributes[c5989db5-b1e1-485b-b09d-a9e50dd1d2cb].Id" id="Variants_IncludedAttributes_c5989db5-b1e1-485b-b09d-a9e50dd1d2cb__Id" class="attribute-id"> 
      </td> 
     </tr> 
    </table> 
</div> 

आप के नाम को देखें, तो पहले तालिका के अंदर क्षेत्र छिपा हुआ है, यह Variants.IncludedAttributes है - जहां यह प्रकार किया जाना चाहिए था [bbd4fdd4-fa22-49f9-8a5e-3ff7e2942126] .IncludedAttributes [...] ...

है ऐसा इसलिए है क्योंकि जब मैं फोन BeginCollectionItem दूसरी बार (शामिल एट्रिब्यूट संग्रह पर) इसके मूल संस्करण के आइटम इंडेक्स मान के बारे में कोई जानकारी नहीं दी गई है।

एक संस्करण प्रतिपादन के लिए मेरे कोड इस तरह दिखता है:

<div class="product-variant round-content-box grid_6" data-id="<%: Model.AttributeType.Id %>"> 
    <h2><%: Model.AttributeType.AttributeTypeName %></h2> 
    <div class="box-content"> 
    <% using (Html.BeginCollectionItem("Variants")) { %> 

     <div class="slot-amount"> 
      <label class="inline" for="slotAmountSelectList"><%: Text.amountOfThisVariant %>:</label> 
      <select id="slotAmountSelectList"><option value="1">1</option><option value="2">2</option></select> 
     </div> 

     <div class="add-values"> 
      <label class="inline" for="txtProductAttributeSearch"><%: Text.addVariantItems %>:</label> 
      <input type="text" id="txtProductAttributeSearch" class="product-attribute-search" /><span><%: Text.or %> <a class="select-from-list-link" href="#select-from-list" data-id="<%: Model.AttributeType.Id %>"><%: Text.selectFromList.ToLowerInvariant() %></a></span> 
      <div class="clear"></div> 
     </div> 
     <%: Html.HiddenFor(m=>m.SlotAmount) %> 

     <div class="included-attributes"> 
      <table> 
       <thead> 
        <tr> 
         <th><%: Text.name %></th> 
         <th style="width: 80px;"><%: Text.price %></th> 
         <th><%: Text.shipping %></th> 
         <th style="width: 90px;"><%: Text.image %></th> 
        </tr> 
       </thead> 
       <tbody> 
        <% for (int i = 0; i < Model.IncludedAttributes.Count; i++) { %> 
         <tr><%: Html.EditorFor(m => m.IncludedAttributes[i]) %></tr> 
        <% } %> 
       </tbody> 
      </table> 
     </div> 

    <% } %> 
    </div> 
</div> 

और एक IncludedAttribute प्रतिपादन के लिए कोड:

<% using (Html.BeginCollectionItem("Variants.IncludedAttributes")) { %> 
    <td> 
     <%: Model.AttributeName %> 
     <%: Html.HiddenFor(m => m.Id, new { @class = "attribute-id" })%> 
     <%: Html.HiddenFor(m => m.ProductAttributeTypeId) %> 
    </td> 
    <td><%: Model.Price.ToCurrencyString() %></td> 
    <td><%: Html.DropDownListFor(m => m.RequiredShippingTypeId, AppData.GetShippingTypesSelectListItems(Model.RequiredShippingTypeId)) %></td> 
    <td><%: Model.ImageId %></td> 
<% } %> 

उत्तर

6

आप MVC 2 और EditorFor उपयोग कर रहे हैं के रूप में, आप की जरूरत नहीं होनी चाहिए स्टीव के समाधान का उपयोग करने के लिए, जो मुझे विश्वास है कि एमवीसी 1 के लिए बस एक काम है। आपको बस कुछ ऐसा करने में सक्षम होना चाहिए:

<% for (int i = 0; i < Model.Variants.Count; i++) { %> 
    <%= Html.DisplayFor(m => m.Variants[i].AttributeType.AttributeTypeName) %> 
    <% for (int j = 0; j < Model.Variants[i].IncludedAttributes.Count; j++) { %> 
     <%= Html.EditorFor(m => m.Variants[i].IncludedAttributes[j]) %> 
    <% } %> 
<% } %> 

कृपया ध्यान दें कि इंडेक्स का उपयोग ... [i] ... [j] ... महत्वपूर्ण है और यह है कि कैसे एमवीसी को पता चलेगा कि आईडी और नामों को सही ढंग से कैसे प्रस्तुत किया जाए।

+8

ओह, एक विवरण बताना भूल गया। मैंने इसे मंजूरी दे दी, क्योंकि मैंने स्टीव के ब्लॉग पोस्ट को पढ़ा है। समस्या यह है कि जावास्क्रिप्ट का उपयोग नए आइटम इंजेक्ट करने के लिए - i और j का मान सिंक से बाहर आता है। स्टीव के समाधान के बारे में यह स्मार्ट बात है, क्योंकि यह एक ग्रिड प्रस्तुत करता है जिसे क्रम में होने की आवश्यकता नहीं है। तो यह एमवीसी 1 के लिए एक कामकाज नहीं है (केवल)। – MartinHN

संबंधित मुद्दे