C#RemoveAt不断删除最后一项

时间:2013-11-27 18:30:32

标签: c# asp.net-mvc-4

似乎无法获取列表以删除正确的项目。 我在代码中放置断点来测试id是否是立即窗口中的shipment.shipmentfreight [id]的正确索引,它显示正确的列表项,但它总是删除最后一项。

在我的视图模型中,我有一个ShipmentFreightLine列表,声明为:

public List<ShipmentFreightLine> shipmentfreight { get; set; }

我有一个回调,删除了返回新部分视图的行:

public virtual ActionResult ShipmentNewFreightLineRemove(int id,NewShipment shipment)
{
    int cnt = shipment.shipmentfreight.Count();

    if (id >= 0 && cnt != 1 && id <= cnt)
    {
        shipment.shipmentfreight.RemoveAt(id);
        //shipment.shipmentfreight.RemoveAt(shipment.shipmentfreight.IndexOf(shipment.shipmentfreight[id]));
        //shipment.shipmentfreight.Remove(shipment.shipmentfreight[id]);
        //shipment.shipmentfreight[id]
    }

    return PartialView("ShipmentNewFreightLineEdit", shipment);

}

正如您所看到的,我已经注释了我尝试过的各种变体,每个变体都删除了列表中的最后一项。

我错过了什么?

我使用C#在VS2012 ASP.NET MVC中编码。


任何帮助都会受到重视

根据Kirk的要求,id字段来自锚

<a href="#" class="deleteRow" onclick="RemoveFreightLine(0);">Remove</a>

调用此javascript函数

function RemoveFreightLine(index) {
    var ShipmentData = $('#fShipmentNew').serialize();
    var url = "/Tracking/ShipmentNewFreightLineRemove/" + index
    $.post(url, ShipmentData,
        function (data) {
            $("#dNewFreightLines").html(data);
        });
}

这些都有正确的ID,所以我不相信它的原因


我跟踪了视图,发现它正在删除列表中的正确项目,但视图显示旧数据

    <table id="tFreight" class ="tblFreight">
    <tbody>
    @for (int i = 0; i < Model.shipmentfreight.Count; i++)
    {
            <tr>
                <td>@(i+1)</td>
                <td>@Html.TextBoxFor(m => m.shipmentfreight[i].Pieces, new { autocomplete = "off", @Class = "txtboxsmall",maxlength ="3"})</td>
                <td>@Html.TextBoxFor(m => m.shipmentfreight[i].Description,new { autocomplete = "off", @Class = "txtareasmall"})</td>
                <td>@Html.TextBoxFor(m => m.shipmentfreight[i].WeightPerPiece, new { autocomplete = "off", @Class = "txtboxsmall",maxlength ="3"})</td>
                <td>@Html.TextBoxFor(m => m.shipmentfreight[i].ActualWeight, new { autocomplete = "off", @Class = "txtboxsmall",maxlength ="3"})</td>
                <td>@Html.TextBoxFor(m => m.shipmentfreight[i].Length, new { autocomplete = "off", @Class = "txtboxsmall",maxlength ="3"})</td>
                <td>@Html.TextBoxFor(m => m.shipmentfreight[i].Width, new { autocomplete = "off", @Class = "txtboxsmall",maxlength ="3"})</td>
                <td>@Html.TextBoxFor(m => m.shipmentfreight[i].Height, new { autocomplete = "off", @Class = "txtboxsmall",maxlength ="3"})</td>
                <td><a href="#" class="deleteRow" onclick="RemoveFreightLine(@(i));">Remove</a></td>
                <td>@Model.shipmentfreight[i].Description</td>
            </tr>
    }
         </tbody>
    </table> 

最后一个单元格用于调试目的,并显示正确的信息。 我正在考虑一些我不知道的TextBoxFor怪癖。

基本上,最后一个单元格显示新列表中的正确数据,但TextBoxFor中的任何内容都显示旧列表数据。 关于我为什么会遇到这种行为的任何想法?


看起来这是ViewData的问题,而不是List。

这似乎解决了这个问题:

ViewData = null;

ModelState.Clear();

找到Why won't a List of complex types bound to TextBoxes in a table show changes to the model in MVC 4?TextBoxFor Helper retains previous value even when model value is empty

的答案

与所有事情一样......找到真正的原因是找到答案的关键。

0 个答案:

没有答案