尝试查找时RowIndex返回null

时间:2019-08-29 18:09:46

标签: c# asp.net-mvc

我正在尝试查找具有单击删除按钮的行的rowIndex。但是,当单击删除按钮时,找不到ID而是返回null和错误。

我已经尝试过了:

$("#delete").on("click", function (e) {


        var row = $(this).closest("tr");
        row.remove();

        return false;
    });

,但此解决方案未发布。然后我也尝试了这个:

<td><input type="submit" formaction="/LansingMileage/DeleteEntry/@item.RowIndex" value="Delete" name="Deletebtn" id="delete" /></td>

这导致我的null问题。

我的控制器:

[HttpPost]
       public ActionResult DeleteEntry(string rowIndex)
        {
           // try
            {
                //rowIndex = Request.Form["Deletebtn"];
                using (db)
                {
                    LansingMileage lansing = db.LansingMileages.Find(rowIndex);
                    db.LansingMileages.Remove(lansing);
                    db.SaveChanges();

                    LansingMileageViewModel model = new LansingMileageViewModel();
                    model.Records = db.LansingMileages.OrderBy(
                        //will need to change to a select all
                        //I'm not following the Take command
                        m => m.RowIndex).ToList();
                    model.SelectedLansingMileage = null;
                    model.DisplayMode = "";

                    return View("Index", model);
                }

            }
           // catch(Exception e)
            {
                ///Debug.WriteLine(e);
            }

        }

我的Index.cshtml:

<table id="LansingData" class="table">
        <thead>
            <tr>
                <th>Action</th>
                <th>Row ID</th>
                <th>Expense Month/Yr</th>
                <th>Travel Date</th>
                <th>Travel Type</th>
            </tr>
        </thead>
        <tbody>
            @foreach (var item in Model.Records)
            {
                if(Model.SelectedLansingMileage != null)
                {
                    if(item.RowIndex == Model.SelectedLansingMileage.RowIndex)
                    {
                        @:<tr class="SelectedLansingMileage">
                    }
                    else
                    {
                        @:<tr>
                    }
                }
                else
                {
                    @:<tr>
                }
                <td><input type="submit" formaction="/LansingMileage/DeleteEntry/@item.RowIndex" value="Delete" name="Deletebtn" id="delete" /></td>
                <td>@item.RowIndex</td>
                <td>@item.ExpMonthYr</td>
                <td>@item.TravelDate</td>
                <td>@item.TripType</td>


                @:</tr>

                @*<tr id="@item.RowIndex">

                      <td><a class="Delete" href="javascript:;">Delete</a></td>
                      <td>@item.ExpMonthYr</td>
                      <td>@item.TravelDate</td>
                      <td>@item.TripType</td>
                      <!--Here is where I will create a table to display my data from the above section-->
                </tr>*@
             }
        </tbody>
    </table>

理想的结果是,当用户单击删除按钮时,它将删除行并发布到数据库。

1 个答案:

答案 0 :(得分:0)

您可以使用Select重载,例如:

@foreach (var item in Model.Select((item,index)=>new{item,index})
{   
    <tr>
      <td>
        <input type="submit" formaction="/LansingMileage/DeleteEntry/@item.index" value="Delete" name="Deletebtn" id="delete" />
      </td>
    </tr>
}

或者像这样手动进行:

@{int RowNo = 0;}
@foreach (var item in Model) {
<tr>
    <td><input type="submit" formaction="/LansingMileage/DeleteEntry/@RowNo" value="Delete" name="Deletebtn" id="delete" /></td>
</tr>

注意:为什么不对行使用@id?如果有可能在删除行时添加行,则可能会发生不良情况。