C# 如何将单击的Jquery数据表行传递给控制器asp.net mvc

C# 如何将单击的Jquery数据表行传递给控制器asp.net mvc,c#,jquery,asp.net,datatables,C#,Jquery,Asp.net,Datatables,我需要在单击行中的某个按钮时将整行(及其数据)发送到控制器方法。我是asp.NETMVC的新手 这是我的表定义: <tr> <td align="center"> <div class="dropdown"> <button onclick="DropdownShow(this)" class="btn btn-default glyphicon glyphicon-picture"></bu

我需要在单击行中的某个按钮时将整行(及其数据)发送到控制器方法。我是asp.NETMVC的新手

这是我的表定义:

<tr>

    <td align="center">
        <div class="dropdown">
            <button onclick="DropdownShow(this)" class="btn btn-default glyphicon glyphicon-picture"></button>
            <div id="@TableRowId" class="dropdown-content">
                <a href="#">Show</a>
                <a href="#">Edit</a>
            </div>
        </div>
    </td>

    <td align="center">
        <a class="btn btn-default"><span class="glyphicon glyphicon-usd"></span></a>
    </td>

    <td>

        @Select Case item.Status
        Case 0
        @<b style="color:red">Cancelled</b>
        Case 1
        @<b style="color:green">OK</b>

        End Select

    </td>

    <td>
        @Html.DisplayFor(Function(modelItem) item.AxCompany)
    </td>

    <td>
        @Html.DisplayFor(Function(modelItem) item.CreditCardType)
    </td>

    <td>
        @Html.DisplayFor(Function(modelItem) item.CreditCardInternalIdentifier)
    </td>

    <td>
        @Html.DisplayFor(Function(modelItem) item.CreditCardExpirationDate)
    </td>

    <td>
        @Html.DisplayFor(Function(modelItem) item.CreditCardIdentifier)
    </td>

</tr>


首先向“SHOW”锚添加一个函数引用,如下所示

<div id="@TableRowId" class="dropdown-content">
    <a onclick="postRow(this)">Show</a>
    <a href="#">Edit</a>
</div>
<tr>
    <td align="center">
        <div class="dropdown">
            <button onclick="DropdownShow(this)" class="btn btn-default     glyphicon glyphicon-picture"></button>
            <div id="@TableRowId" class="dropdown-content">
                <a href="#">Show</a>
                <a href="#">Edit</a>
            </div>
        </div>
    </td>

    <td align="center" id="td1">
    <a class="btn btn-default"><span class="glyphicon glyphicon-usd"></span>    </a>
    </td>

    <td id="td2">

        @Select Case item.Status
        Case 0
        @<b style="color:red">Cancelled</b>
        Case 1
        @<b style="color:green">OK</b>

        End Select

    </td>

    <td id="td3">
        @Html.DisplayFor(Function(modelItem) item.AxCompany)
    </td>

    <td id="td4">
        @Html.DisplayFor(Function(modelItem) item.CreditCardType)
    </td>

    <td id="td5">
        @Html.DisplayFor(Function(modelItem) item.CreditCardInternalIdentifier)
    </td>

    <td id="td6">
        @Html.DisplayFor(Function(modelItem) item.CreditCardExpirationDate)
    </td>

    <td id="td7">
        @Html.DisplayFor(Function(modelItem) item.CreditCardIdentifier)
    </td>
</tr>
方式2示例

[HttpPost]
public JsonResult catchRowData(string td1, string td2, string td3, string td4, string td5, string td6, string td7)
{
     // put your codes 
     return View();
}
第三,需要postRow函数(将其放在jquery引用之后的表视图中)


函数postRow(tr){
变量数据={};
$(tr).最近的(“tr”).find(“td[id]”)。每个(函数(ind,td){
$(data.attr($(td.attr(“id”),$(td.html());
});
$.post(“/yourController/catchRowData”,data).done(函数(结果){
//玩弄结果:)
}).fail(函数(){
警惕(“某些事情出错!”;//可能找不到操作
});
}

最后,如果要使用ActionResult或PartialViewResult而不是JsonResult,则需要创建一个视图。另外,如果您使用ActionResult,您必须在提交表单时发布数据。

如果我给每个id“td1”、“td”2等等,请查看一下,这并不好,因为我有很多行,我不需要迭代器变量作为id?您需要td或子元素中的属性名称。可能您可以使用Html.LabelFor代替Html.DisplayFor。。然后,您可以从标签名称中获取属性名称。。试试这个。
[HttpPost]
public JsonResult catchRowData(string td1, string td2, string td3, string td4, string td5, string td6, string td7)
{
     // put your codes 
     return View();
}
<script>
    function postRow(tr){
        var data={};

        $(tr).closest("tr").find("td[id]").each(function(ind,td){

             $(data).attr($(td).attr("id"), $(td).html() );
        });

        $.post("/yourController/catchRowData",data).done(function(result){

            // play with result :)

        }).fail(function(){

            alert("some things wrong!"); // probably could't find the action

        });
    }
</script>