2013-03-23 39 views
0

我有這個模型ASP.NET MVC - 無法綁定表單值列出<Model>

public class ItemClassification 
    { 
     public int ItemID {get; set;} 
     public string ItemName {get; set;} 
    } 

,我有這種形式

<form method="POST" action="/Home/UpdateItemClassifications"> 
     <table> 
      <tr> 
        <th>Item ID</th> 
        <th>Item Name</th> 
      </tr> 
      <tr> 
        <td> 
         <input name="ItemClassification[0].ItemID" value="1"/> 
        </td> 
        <td> 
         <input name="ItemClassification[0].ItemName" value="Item One"/> 
        </td> 
      </tr> 
      <tr> 
        <td> 
         <input name="ItemClassification[1].ItemID" value="2"/> 
        </td> 
        <td> 
         <input name="ItemClassification[1].ItemName" value="Item Two"/> 
        </td> 
      </tr> 
      <tr> 
        <td> 
         <input name="ItemClassification[2].ItemID" value="3"/> 
        </td> 
        <td> 
         <input name="ItemClassification[2].ItemName" value="Item Three"/> 
        </td> 
      </tr> 
     </table> 
    </form> 

而且我想要綁定這些值來這控制器動作循環它們並在數據庫中更新它們。

[HttpPost] 
    public ActionResult UpdateItemClassifications(List<ItemClassification> UpdatedClassifications) 
    { 
     //save logic here.. 
    } 

無論出於何種原因,我沒有獲取綁定到ItemClassification模型的發佈表單值。任何幫助或方向將不勝感激。我試圖用僅有一組值的方式發佈此表單,並從控制器中刪除了「列表<>」,並正確映射了這些值。

+1

如果它們是按順序排列,請嘗試刪除「ItemClassification」並放置[0] .ItemID – Sunny 2013-03-23 19:05:39

+1

如果它們沒有按順序排列,則需要在每個項目前都有隱藏值攜帶索引,請檢查http:// haacked。 COM /存檔/ 2008/10/23 /模型綁定到一個-list.aspx – Sunny 2013-03-23 19:08:39

回答

0

原來我在[0] .ItemID之前不需要「ItemClassification」,謝謝你的回答Sundeep。我可能確實需要查看您引用的被黑客入侵的文章,因爲我可能會爲此添加JavaScript刪除功能,這無疑會改變我的索引。

相關問題