我正嘗試使用RemoveAt從通用列表中刪除項目。奇怪的是,在使用調試器的時候,我可以看到我的物品已被移除,但是當它傳遞給視圖時,移除的物品顯示出來,但最後一個物品已被移除。從通用列表中刪除項目
代碼看起來像這樣
public ActionResult(MyModel model, int[] removeitems)
{
//model.ListItems has 10 items
//Incoming removeitems has 0 as the first item to remove as a test
foreach(int item in removeitems)
{
model.ListItems.RemoveAt(item);
}
//by this time debugger shows that item 0 has in fact been removed and no longer exists in the list
return View(model);
//after the view is rendered it shows item 0 is still there but 10 has been removed
}
我知道我可以通過複製項目到另一個列表等做另一種方式,但所有的測試表明,上面的代碼不會刪除的第一個項目,但認爲不不反映這一點。
任何想法?
後您的視圖代碼....我認爲,有鑑於代碼一些邪惡的一部分.... – 2013-05-12 05:42:15
你確定你不想' .Remove()'找到元素並將其刪除?那麼找到第一個匹配元素並將其刪除。 – Nomad101 2013-05-12 05:44:25
@ Nomad101我感謝我知道刪除它不是我想要的 – AliK 2013-05-12 05:49:03