简体   繁体   English

如何使用ActionLink作为提交按钮删除表中的每一行?

[英]How to use ActionLink as submit button to Delete each row in a table?

I am developing a application where I need display details like ID, name and address of customers. 我正在开发一个需要显示详细信息(例如ID,客户名称和地址)的应用程序。 And one should be able to edit or delete each customer details. 并且应该能够编辑或删除每个客户的详细信息。 For that I have added Edit and delete action links as buttons for reach row. 为此,我添加了“编辑”和“删除操作”链接作为覆盖范围行的按钮。 In my controller I have added switch case to perform edit or delete actions. 在我的控制器中,我添加了开关盒以执行编辑或删除操作。

My question is how to use action link as submit button. 我的问题是如何使用动作链接作为提交按钮。 Or is there any better way to perform edit and delete records 还是有更好的方法来执行编辑和删除记录

这是我前端的样子

View : 查看:

 <table class="table"> <tr> <th> @Html.DisplayNameFor(model => model.id) </th> <th> @Html.DisplayNameFor(model => model.name) </th> <th> @Html.DisplayNameFor(model => model.address) </th> <th></th> </tr> @foreach (var item in Model) { <tr> <td> @Html.DisplayFor(modelItem => item.id) </td> <td> @Html.DisplayFor(modelItem => item.name) </td> <td> @Html.DisplayFor(modelItem => item.address) </td> <td> @Html.ActionLink("Edit", "", new { id = item.id }) | @Html.ActionLink("Delete", "", new { id = item.id }) </td> </tr> } </table> 

Controller : 控制器:

[HttpPost] [HttpPost]

    public ActionResult MyTableView(int id, List<MyList> list)
    {

        switch (submitButton)
        {
            case "Delete":


            break;



            case "Edit" :



            break;

        }

        return View ("MyTableView");
    }

This is a method I often use since you asked if there is any different/better method. 这是我经常使用的方法,因为您询问是否有其他/更好的方法。 FYI This example is using FontAwesome Icons and Bootstrap 4 styling for the buttons. 仅供参考,此示例对按钮使用FontAwesome Icons和Bootstrap 4样式。

I like to use Ajax and then update the frontend in one way or another depending on response. 我喜欢使用Ajax,然后根据响应以一种或另一种方式更新前端。

Then if a user wants to edit a record I take them to a different page to edit that specific entry where I just use a standard viewmodel and razor form / @Html.EditorFor() stuffs 然后,如果用户想要编辑一条记录,我@Html.EditorFor()其带到另一个页面以编辑该特定条目,在这里我只使用标准的viewmodel和剃刀形式/ @Html.EditorFor()东西

You'll notice the HTML has an @Item.Id which would be from an iteration of the viewmodel list data in a @foreach(var item in list) That's where the Id value in javascript is coming from. 您会注意到HTML具有@Item.Id ,它来自@foreach(var item in list)列表中的@foreach(var item in list) viewmodel列表数据的迭代,这就是javascript中的ID值的来源。

Controller 控制者

    public JsonResult Delete(int? id)
    {
        if (id == null)
        {
            return Json(-1);
        }
        var document = CommonService.GetDocument(id);
        if (document == null)
        {
            return Json(-1);
        }
        if (0 == AdminService.DeleteDocument((int)id))
        {
            return Json(1);
        }
        return Json(-1);
    }

Html HTML

Have a row in the table like 在表中像这样

<td class="text-nowrap"><a class="btn btn-primary text-white" href="@Url.Action("Edit", "Controller", new { id = item.Id })"><i class="fa fa-pencil-square-o"></i>&nbsp;Edit</a>  &nbsp;&nbsp; <button value="@item.Id" class="btn btn-primary text-white delete"><i class="fa fa-trash-o"></i>&nbsp;Delete</button></td>

Javascript Java脚本

$(".delete").unbind().click(function () {
        var entryId = $(this).val();
        if (confirm("Are you sure you want to delete?"))
        {
            var selected = $(this).parents("tr").addClass("selected");
            $.ajax(
            {
                url: '@Url.Action("Delete", "Controller")',
                type: "POST",
                dataType: "json",
                data: { id: entryId }
            })
            .done(function (data) {
                if (data == 1) {
                    table.row(".selected").remove().draw(false); // Delete row from view
                }
                else {
                    Alert("Something went wrong...")
                }
            });
        }
    });

As @CodingYoshi suggested I did like this. 正如@CodingYoshi所建议的那样,我确实这样做了。

view : 查看:

  <td> @Html.ActionLink("Edit", "EditDetails", new { id = item.id }) | @Html.ActionLink("Delete", "Delete", new { id = item.id }) </td> 

When the use click on any link going to that action controller 当使用时单击指向该动作控制器的任何链接

 public ActionResult  Delete(int id)

{ {

//code to delete //要删除的代码

} }

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

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