简体   繁体   English

如何在不刷新页面的情况下从表中删除行?

[英]How can i delete a row from table without refreshing the Page?

How can i delete a row from table without refreshing the Page? 如何在不刷新页面的情况下从表中删除行? I deleted the delete view from the views but it is showing me "The Resources Cannot Be Found" I am not so perfect in MVC if someone could help me with this,that will be so thankful. 我从视图中删除了删除视图,但显示的是“找不到资源”如果有人可以帮助我,我在MVC中并不是那么完美,这将非常感激。

Here is my Controller 这是我的控制器

[Here is my Index view [这是我的索引视图

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

  1. Remove Action Link:- @Html.ActionLink("Edit", "Edit", new { id = item.Id }) 删除操作链接:-@ Html.ActionLink(“ Edit”,“ Edit”,新的{id = item.Id})
  2. Put this item.id in hidden field 将此item.id放在隐藏字段中
  3. Get the id from hidden field in a javascript variable. 从javascript变量中的隐藏字段获取ID。
  4. Call your delete action method via ajax[POST] call, pass id in ajax call. 通过ajax [POST]调用来调用您的delete动作方法,在ajax调用中传递ID。

Hope it will help! 希望对您有所帮助!

Simply use JavaScript and Ajax for it. 只需使用JavaScript和Ajax。 Try this approach in your DeleteView: 在DeleteView中尝试这种方法:

<script>
function DeleteId(id)
{
  $.ajax({
     dataType: "json",
     url: '@Url.Action("ActionName","ControllerName")',
     data: {id: id},
     success: function(result){
                //todo
            }
       });
    }
</script>

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

            <!-- Code Removed for simplicity -->
            <a onClick="DeleteId('@item.Id');"> Delete <a/>
            </td>
        </tr>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM