javascript - 如何在不刷新页面的情况下从表中删除一行?
问题描述
如何在不刷新页面的情况下从表中删除一行?我从视图中删除了删除视图,但它向我显示“找不到资源”如果有人可以帮助我,我在 MVC 中并不是那么完美,那将非常感激。
[这是我的索引视图
<table id="customers">
<tr>
<th style="text-align:center">
@Html.DisplayNameFor(model => model.Name)
</th>
<th style="text-align:center">
@Html.DisplayNameFor(model => model.Salary)
</th>
<th style="text-align:center">
@Html.DisplayNameFor(model => model.Address)
</th>
<th style="text-align:center">Action</th>
</tr>
@foreach (var item in Model)
{
<tr class="centerAlign">
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Salary)
</td>
<td>
@Html.DisplayFor(modelItem => item.Address)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.Id }) |
@Html.ActionLink("Details", "Details", new { id = item.Id }) |
@Html.ActionLink("Delete", "Delete", new { id = item.Id })
</td>
</tr>
public ActionResult Delete(int id = 0)
{
New_Table new_table = db.New_Table.Find(id);
if (new_table == null)
{
return HttpNotFound();
}
return View(new_table);
}
//
//POST: /Default1/Delete/5
[HttpPost]
public ActionResult Delete(int id)
{
New_Table new_table = db.New_Table.FirstOrDefault(s => s.Id.Equals(id));
if (new_table != null)
{
db.DeleteObject(new_table);
db.SaveChanges();
}
return View("Index");
}
] 2
解决方案
- 删除操作链接:- @Html.ActionLink("Edit", "Edit", new { id = item.Id })
- 将此 item.id 放在隐藏字段中
- 从 javascript 变量中的隐藏字段中获取 id。
- 通过 ajax[POST] 调用调用您的删除操作方法,在 ajax 调用中传递 id。
希望它会有所帮助!
推荐阅读
- oracle - oracle如何查找某条记录的索引分区
- html - 学习者问题:桌子很糟糕。如何使用 HTML 和 CSS 在表格上设置总计字段
- javascript - onClick 中的反应状态引用是错误的 - 可能的关闭问题?
- python - 如何通过另一个数据框中的键过滤数据框中的列
- python - 有没有办法可以简化这个矩形绘图代码?
- c++ - << 对象的运算符重载在访问重载方法中的属性时会导致错误
- python - 无法删除通过 python os.mkdir 创建的文件夹
- react-native - 跨平台开发:如何缓解依赖 3rd 方插件实现原生功能的问题?
- java - 删除java应用程序中sql server数据库的所有数据
- slack - 使用 slack blocks api 创建一个链接按钮