2012-02-17 74 views
0

我希望用戶能夠一下子讓我創造了這樣的形式更新多行(我剝離出來的HTML,以顯示我真正的意思):如何重新計算形式陣列

@using(Html.BeginForm()) { 

    @{int i = 0; } 
    @foreach(var row in Model) { 
     @Html.TextBox("Customer["+ i +"].CustomerID") 
     @Html.TextBox("Customer["+ i +"].Name") 
    } 

    <input type="submit" /> 
} 

在HTML的輸出是(3行):

<input type="text" name="Customer[0].CustomerID" /> 
<input type="text" name="Customer[0].Name" /> 

<input type="text" name="Customer[1].CustomerID" /> 
<input type="text" name="Customer[1].Name" /> 

<input type="text" name="Customer[2].CustomerID" /> 
<input type="text" name="Customer[2].Name" /> 

當用戶點擊提交按鈕,所述數據被髮送到一個控制器:

Public ActionResult EditCustomer(IEnumerable<Customer> Customer) { 
} 

其中給出我有一個很好的IEnumerable與所有輸入的數據,我可以做任何我想要的。它按預期工作。

現在我想進一步,讓用戶刪除行。

此失敗

因爲當我刪除第二行中,該陣列是不再「封閉」:它從客戶[0]到客戶[2]變爲和ASP.NET MVC具有的問題。

所以我認爲可以通過重新計算索引來解決。但我怎麼在jQuery中做到這一點?

因此,在中間行被刪除後,將運行一個函數,將Customer[2].CustomerID更新爲Customer[1].CustomerID

回答

1

我會推薦你​​following article它說明了一個很好的幫手,你可以用它來實現,而不是使用魔術字符串。

+0

非常感謝!這正是我正在尋找的 – jao 2012-02-17 14:18:11

+0

嗯,新問題。主鍵被重置爲零(http://stackoverflow.com/questions/9330598/primary-key-set-to-zero-on-submit-of-ienumerablet) – jao 2012-02-17 15:09:05