Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 将对象参数从javascript\ajax传递到控制器中的函数_Asp.net Mvc_Asp.net Core - Fatal编程技术网

Asp.net mvc 将对象参数从javascript\ajax传递到控制器中的函数

Asp.net mvc 将对象参数从javascript\ajax传递到控制器中的函数,asp.net-mvc,asp.net-core,Asp.net Mvc,Asp.net Core,我有一个表,我希望能够更新复选框所在的每一行的状态 (见所附屏幕截图) 模型中的复选框属性未映射到数据库([未映射]) Html: html格式的表格: <table id="tblData" class="table table-striped table-bordered" style="width:100%"> <thead class="thead-dark">

我有一个表,我希望能够更新复选框所在的每一行的状态 (见所附屏幕截图)

模型中的复选框属性未映射到数据库([未映射]) Html:

html格式的表格:

<table id="tblData" class="table table-striped table-bordered" style="width:100%">
    <thead class="thead-dark">
        <tr class="table-info">
            <th>Address</th>
            <th>Payment Type</th>
            <th>Amount</th>
            <th>Payment Date</th>
            <th>Status</th>
            <th></th>
    </thead>
    @foreach (PaymentHistory paymentHistory in Model)
    {
        <tr>
   <td>@ViewBag.getPaymentAddress(paymentHistory.SentFromAddressId).ToString()</td>     <td>@ViewBag.getPaymentType(paymentHistory.SentFromAddressId).ToString()</td>
            <td>@paymentHistory.Amount$</td>
            <td>@paymentHistory.PayDate</td>
            <td>@paymentHistory.Status</td>
            @if (paymentHistory.Status != "Approved")
            {
                <td>
                    <div class="text-center">
                        <input type="checkbox" asp-for="@paymentHistory.isChecked"/>
                    </div>
                </td>
            }
            else
            {
                <td></td>
            }
        </tr>
    }
</table>

地址
支付类型
数量
付款日期
地位
@foreach(模型中的PaymentHistory PaymentHistory)
{
@ViewBag.getPaymentAddress(paymentHistory.SentFromAddressId).ToString()@ViewBag.getPaymentType(paymentHistory.SentFromAddressId).ToString()
@付款历史。金额$
@paymentHistory.PayDate
@支付历史。状态
@如果(paymentHistory.Status!=“已批准”)
{
}
其他的
{
}
}
我唯一的问题是,我想将对象作为参数从视图(包含复选框的行和状态)传递到控制器中的函数, 你知道我该怎么做吗?
谢谢

获取数组中的所有复选框id,使用该数组更新表

我想将对象作为参数从视图(包含复选框的行和状态)传递到控制器中的函数,有什么办法吗

为了满足您的需求,您可以尝试为
SentFromAddressId
字段添加一个隐藏字段,如下所示

<td>
    <div class="text-center">
        <input type="hidden" asp-for="@paymentHistory.SentFromAddressId" />
        <input type="checkbox" asp-for="@paymentHistory.isChecked" />
    </div>
</td>
并使用以下代码片段将数据发布到action方法

$.ajax({
    type: 'POST',
    url: '/PaymentHistory/ApproveStatus',
    data: pdata,
    processData: false,
    contentType: false,
    datatype: 'json',
    success: function (res) {
        //...
    }
});
适宜作用法


你能分享一下复选框的html是如何组合在一起的吗?添加到帖子中,谢谢。这是我的问题,如何将id数组从javaxcript传递到Ajax,然后传递到控制器:)你可以尝试为
SentFromAddressId
添加一个隐藏字段,这样你就可以为每个选中的行获取它。太棒了,谢谢!
<table id="tblData" class="table table-striped table-bordered" style="width:100%">
    <thead class="thead-dark">
        <tr class="table-info">
            <th>Address</th>
            <th>Payment Type</th>
            <th>Amount</th>
            <th>Payment Date</th>
            <th>Status</th>
            <th></th>
    </thead>
    @foreach (PaymentHistory paymentHistory in Model)
    {
        <tr>
   <td>@ViewBag.getPaymentAddress(paymentHistory.SentFromAddressId).ToString()</td>     <td>@ViewBag.getPaymentType(paymentHistory.SentFromAddressId).ToString()</td>
            <td>@paymentHistory.Amount$</td>
            <td>@paymentHistory.PayDate</td>
            <td>@paymentHistory.Status</td>
            @if (paymentHistory.Status != "Approved")
            {
                <td>
                    <div class="text-center">
                        <input type="checkbox" asp-for="@paymentHistory.isChecked"/>
                    </div>
                </td>
            }
            else
            {
                <td></td>
            }
        </tr>
    }
</table>
<td>
    <div class="text-center">
        <input type="hidden" asp-for="@paymentHistory.SentFromAddressId" />
        <input type="checkbox" asp-for="@paymentHistory.isChecked" />
    </div>
</td>
var pdata = new FormData();

$("input[name='paymentHistory.isChecked']:checked").each(function (index, el) {
    var sentFromAddressId = $(this).siblings("input[type='hidden']").val();
    pdata.append("Ids", sentFromAddressId);
})
$.ajax({
    type: 'POST',
    url: '/PaymentHistory/ApproveStatus',
    data: pdata,
    processData: false,
    contentType: false,
    datatype: 'json',
    success: function (res) {
        //...
    }
});
public IActionResult ApproveStatus(int[] Ids)
{
    //code logic here
    //update corresponding record based on id within Ids