简体   繁体   English

如何使用Javascript将特定数据从Webgrid传递到MVC Controller?

[英]How to pass specific data from webgrid into MVC Controller using Javascript?

I'm trying to delete a specific row in my webgrid table using Javascript and Entity Framework. 我正在尝试使用Javascript和Entity Framework删除Webgrid表中的特定行。 This is what i got so far: 这是我到目前为止所得到的:

在此处输入图片说明

The webgrid view works, I've added a (red cross) tag in the end for deleting this specific row. webgrid视图有效,最后我添加了一个(红叉)标记以删除此特定行。

HTML: HTML:

<div class="row">
        <div class="col-lg-12 d-flex align-items-stretch">
            @grid.Table(tableStyle: "table table-responsive table-striped table-bordered",
                columns: grid.Columns(
                  grid.Column(columnName: "ApiRedirectID", header: "ID", format:@<text><div class="edit" data-id="@item.ApiRedirectID" data-propertyname="ApiRedirectID">@item.ApiRedirectID</div></text>),
                  grid.Column(columnName: "ApiName", header: "Name", format:@<text><div class="edit" data-id="@item.ApiRedirectID" data-propertyname="ApiName">@item.ApiName</div></text>),
                  grid.Column(columnName: "CompanyID", header: "Company ID", format:@<text><div class="edit" data-id="@item.ApiRedirectID" data-propertyname="CompanyID">@item.CompanyID</div></text>),
                  grid.Column(columnName: "Company.CompanyName", header: "Company Name", format:@<text><div class="edit" data-id="@item.ApiRedirectID" data-propertyname="Company.CompanyName">@item.Company.CompanyName</div></text>),
                  grid.Column(columnName: "ApiURL2", header: "URL", format:@<text><div class="edit" data-id="@item.ApiRedirectID" data-propertyname="ApiURL2">@item.ApiURL2</div></text>),
                  grid.Column(columnName: "Delete", header: " ", format:@<a href="#" class="display delete-btn"><span class="glyphicon glyphicon-remove" style="color:red;"></span></a>)
          )
     )

        </div>
        <div class="col-lg-12 d-flex align-items-stretch">
            @grid.PagerList(mode: WebGridPagerModes.All, paginationStyle: "pagination pagination-small pagination-right")
        </div>
</div>

Javascript (what I came up with so far): Javascript(到目前为止我想出了什么):

 $('.delete-btn').on("click", function () {

       //how to get specific row id here and pass it to my controller?     
 })

What I'm trying to achieve is that I pass the ApiRedirectID of the row I want to delete to Javascript and than to my MVC Controller. 我想要实现的是将我要删除的行的ApiRedirectID传递给Javascript,而不是MVC Controller。

Hope someone can help! 希望有人能帮忙!

Thanks in advance! 提前致谢!

Modify this line create an anchor tag to pass id and the best part is it will append id of each with anchor tag and you will get in controller directly 修改此行,创建一个锚标记以传递ID,最好的部分是它将每个锚的ID附加到锚标记之后,您将直接进入控制器

(**In your case @item.Id From which you can find the record and delete it )** (**对于您的@ item.Id,您可以从中找到记录并删除它 )**

to the controller, so in the controller side, you will get id to remove it from DB. 到控制器,因此在控制器端,您将获得ID以将其从数据库中删除。

grid.Column(columnName: "Delete", header: " ", format:@<a href="Controller/action/id" class="display delete-btn"><span class="glyphicon glyphicon-remove" style="color:red;"></span></a>)

Assuming each row is a <tr> , you can do something like. 假设每一行都是一个<tr> ,则可以执行类似的操作。

$('.delete-btn').on("click", "body", function () {
        var $row = $(this).closest('tr');
        var rowid = $row.find('[data-propertyname="ApiRedirectID"]');
        $.ajax({
          method: "POST",
          url: "controller/deleterow",
          data: { id: rowid }
        })
          .done(function( msg ) {
            alert( "Row Deleted!" );
      });
 });

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

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