gpt4 book ai didi

c# - 尝试查找时 RowIndex 返回 null

转载 作者:行者123 更新时间:2023-11-30 18:11:34 26 4
gpt4 key购买 nike

我试图找到具有被单击的删除按钮的行的 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>

这导致了我的空问题。

我的 Controller :

[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);
}

}

我的索引.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>

期望的结果是,当用户单击删除按钮时,它会删除该行并发布到数据库。

最佳答案

您可以使用 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?如果有可能在删除行时添加行,则可能会发生不好的事情。

关于c# - 尝试查找时 RowIndex 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57715724/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com