2016-08-01 26 views
1

我有一個視圖模型包含public List<Room> Rooms { get; set; }房間包含一個List<BL.Image> Images。在我看來,我循環穿過房間並顯示每個圖像。ASP MVC通過非順序項目

for (int i = 0; i < Model.Rooms.Count; i++) 
{ 
    @for (var ii = 0; ii < Model.Rooms[i].Images.Count; ii++) 
    { 
     <div class="slide"> 
      <img class="pic panel" src="@Url.Content(Model.Rooms[i].Images.ToList()[ii].Path)" /> 
      @{ ViewBag.Imageindex = "Rooms[" + i + "].Images[" + ii + "].Index"; } 
      <input type="hidden" name="@ViewBag.ImageIndex" value="@Html.Raw(ii)" /> 
@*also tried<input type="hidden" name="@ViewBag.ImageIndex" value="" />also tried*@ 

      @Html.HiddenFor(m => Model.Rooms[i].Images.ToList()[ii].ImageID, new { Name = "Rooms[" + i + "].Images[" + ii + "].ImageID" }) 
      @Html.HiddenFor(m => Model.Rooms[i].Images.ToList()[ii].Path, new { Name = "Rooms[" + i + "].Images[" + ii + "].Path" }) 
      <div class="snipit"> 
       <img class="hoverpic panel" src="http://findicons.com/files/icons/99/office/128/delete.png" width="40" height="40" /> 
      </div> 
     </div> 
    } 
} 

圖像可以被添加或由用戶刪除,但如果圖像被刪除,當表單被刪除的圖像之前僅提交圖像被例如識別

  • 圖像1,圖像2,圖像3,圖像4顯示

  • 圖像3被刪除

  • 僅提交當表單圖像1和2中確認。

我爲解決這個問題而回顧的問題之一是this one,我似乎也在做同樣的事情。這個問題還鏈接到一個Blog Post在這 - 臨近結束它涵蓋了非連續指數

非循序索引 ...當你不能保證提交的值將維持,會發生什麼順序索引?例如,假設您希望允許在通過JavaScript提交書籍列表之前刪除行。 好消息是,通過引入額外的隱藏輸入,您可以允許任意索引。在下面的例子中,我們爲每個需要綁定到列表的項目提供一個帶有.Index後綴的隱藏輸入。這些隱藏輸入中的每一個的名稱都是相同的......這將給模型綁定器一個很好的索引集合,以便在綁定到列表時查找。

<form method="post" action="/Home/Create"> 

<input type="hidden" name="products.Index" value="cold" /> 
<input type="text" name="products[cold].Name" value="Beer" /> 
<input type="text" name="products[cold].Price" value="7.32" /> 

<input type="hidden" name="products.Index" value="123" /> 
<input type="text" name="products[123].Name" value="Chips" /> 
<input type="text" name="products[123].Price" value="2.23" /> 

<input type="hidden" name="products.Index" value="caliente" /> 
<input type="text" name="products[caliente].Name" value="Salsa" /> 
<input type="text" name="products[caliente].Price" value="1.23" /> 

<input type="submit" /> 

不幸的是,我們沒有產生這些隱藏輸入的幫手。

我在做什麼錯?

回答

2

您需要2個隱藏的輸入,用於名稱爲Rooms.IndexRooms[i].Images.Index的收集索引器。另外,請勿嘗試更改由HtmlHelper方法生成的name屬性。你的代碼應該是

for (int i = 0; i < Model.Rooms.Count; i++) 
{ 
    <input type="hidden" name="Rooms.Index" value="@i" /> // outer indexer 
    @for (var ii = 0; ii < Model.Rooms[i].Images.Count; ii++) 
    { 
     <input type="hidden" name="Rooms[@i].Images.Index" value="@ii" /> // inner indexer 
     <div class="slide"> 
      <img class="pic panel" src="@Url.Content(Model.Rooms[i].Images.ToList()[ii].Path)" /> 
      @Html.HiddenFor(m => Model.Rooms[i].Images[ii].ImageID) 
      @Html.HiddenFor(m => Model.Rooms[i].Images[ii].Path) 
      <div class="snipit"> 
       <img class="hoverpic panel" src="http://findicons.com/files/icons/99/office/128/delete.png" width="40" height="40" /> 
      </div> 
     </div> 
    } 
} 
+0

謝謝你的回答,它已經工作(非常安心)。關於不改變名稱元素,我只是作爲最後的手段。在改變它之前,'name =「[4] .Path」'被渲染(例如)並且圖像被傳遞爲空。我可以在鏈條上進一步做錯嗎,還是這個問題需要一個新的線程? – tony09uk

+0

剛剛注意到你有'.ToList()'在那裏需要被刪除(見編輯) –

+0

對不起,我把'List '它應該是'ICollection',因此索引不會被應用而不會將其轉換爲列表。 'Room'類是由實體框架生成的,所以我認爲我堅持它是一個集合,我認爲 – tony09uk