首页 > 解决方案 > 如何在不刷新页面的情况下从表中删除一行?

问题描述

如何在不刷新页面的情况下从表中删除一行?我从视图中删除了删除视图,但它向我显示“找不到资源”如果有人可以帮助我,我在 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

标签: javascriptc#jqueryajaxasp.net-mvc

解决方案


  1. 删除操作链接:- @Html.ActionLink("Edit", "Edit", new { id = item.Id })
  2. 将此 item.id 放在隐藏字段中
  3. 从 javascript 变量中的隐藏字段中获取 id。
  4. 通过 ajax[POST] 调用调用您的删除操作方法,在 ajax 调用中传递 id。

希望它会有所帮助!


推荐阅读