C# C RemoveAt不断删除最后一项

C# C RemoveAt不断删除最后一项,c#,asp.net-mvc-4,C#,Asp.net Mvc 4,似乎无法获取列表以删除正确的项目。 我在代码中设置了断点,以测试id是否是即时窗口中Shipping.shipmentfreight[id]的正确索引,它显示正确的列表项,但总是删除最后一项 在我的视图模型中,ShipmentFreightLine的列表声明为: public List<ShipmentFreightLine> shipmentfreight { get; set; } 这些都有正确的身份证,所以我不相信这是原因 我跟踪了视图,发现它正在删除列表中的正确项,但视图显

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

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

public List<ShipmentFreightLine> shipmentfreight { get; set; }
这些都有正确的身份证,所以我不相信这是原因

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

    <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> 

用和找到了答案


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

所有事情都是基于id的值进行预测的,但是,您没有显示指示其值的代码。如果列表中只有1项,并且id=0 id=1应该引发异常,那么这不会删除任何内容吗?Kirk-其值是由javascript调用设置的值,其值是正确的,但我将发布itAustin-其构建为删除1,不再进一步。然而,我认为如果id>=0&&cnt!=1和&id
<a href="#" class="deleteRow" onclick="RemoveFreightLine(0);">Remove</a>
function RemoveFreightLine(index) {
    var ShipmentData = $('#fShipmentNew').serialize();
    var url = "/Tracking/ShipmentNewFreightLineRemove/" + index
    $.post(url, ShipmentData,
        function (data) {
            $("#dNewFreightLines").html(data);
        });
}
    <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> 
ViewData = null;
ModelState.Clear();