亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從復選框中獲取值并作為列表發布到控制器

從復選框中獲取值并作為列表發布到控制器

C#
一只名叫tom的貓 2022-01-09 16:53:59
檢查下面的代碼。我正在ProductId向我的控制器發布一個列表,并希望使用名為- 的視圖模型接收它UpdateProductStatus。但我當前代碼的問題是:ajax 成功將其發布到控制器,但UpdateProductStatus無法獲取ProductId. 這總是返回 null。我在這里做錯了什么?我認為可能在 ajax 中我做錯了。我該如何解決這個問題以將所有內容ProductId作為列表表單控制器接收?提前致謝控制器:[HttpPost]        public IActionResult UpdateProductStatus([FromBody]List<UpdateProductStatus> UpdateProductStatus)        {            return Json("ok");        }查看型號:using System;using System.Collections.Generic;using System.Linq;using System.Threading.Tasks;namespace BlexzWeb.ViewModels{    public class UpdateProductStatus    {        public int ProductId { get; set; }    }}查詢: $('#btnActivate').on('click', function () {            var allSelectedProductId = [];            var allSelectedProductIdWithKey = [];            $('.chkItems:checked').each(function() {                allSelectedProductId.push($(this).val());            });            for (var _allSelectedProductId in allSelectedProductId) {                allSelectedProductIdWithKey.push('ProductId'+':'+_allSelectedProductId);            }            console.log(allSelectedProductIdWithKey);            //var things = JSON.stringify(allSelectedProductIdWithKey);            var things = JSON.stringify({ 'UpdateProductStatus': allSelectedProductIdWithKey });            console.log(things);            $.ajax({                contentType: 'application/json; charset=utf-8',                dataType: 'json',                type: 'POST',                url: '/Products/UpdateProductStatus',                data: things,                success: function () {                    console.log('sssss');                },                failure: function (response) {                    console.log('fffff');                }            });html:                                        <input type="checkbox" class="chkItems" value="1">                                        <input type="checkbox" class="chkItems" value="2"><button id="btnActivate">Button</button>
查看完整描述

2 回答

?
德瑪西亞99

TA貢獻1770條經驗 獲得超3個贊

要綁定到您的控制器方法,您需要發送一個包含名稱/值對的對象數組ProductId。要構建對象數組,請使用


$('#btnActivate').on('click', function () {

    var allSelectedProductId = [];

    $('.chkItems:checked').each(function() {

        allSelectedProductId.push({ ProductId: $(this).val() });

    });

    var things = JSON.stringify({ UpdateProductStatus: allSelectedProductId });


    $.ajax({

        contentType: 'application/json; charset=utf-8',

        dataType: 'json',

        type: 'POST',

        url: '/Products/UpdateProductStatus',

        data: things,

        success: function () {

            ....

    });

});


查看完整回答
反對 回復 2022-01-09
?
呼如林

TA貢獻1798條經驗 獲得超3個贊

您當前的代碼正在為 ajax 調用發送如下所示的有效負載。


{"UpdateProductStatus":["ProductId:0","ProductId:1"]}

您的操作方法參數是UpdateProductStatus對象列表。因此,要使模型綁定與您當前的操作方法參數簽名正常工作,您的有效負載應如下所示。


[{"ProductId":"1"},{"ProductId":"2"}]

無需指定參數名稱。只需傳遞一個項目數組,每個項目都有一個ProductId屬性和它的值。


var allSelectedProductIdWithKey = [];

$('.chkItems:checked').each(function () {

    allSelectedProductIdWithKey.push({ ProductId: $(this).val() });

});


var things = JSON.stringify(allSelectedProductIdWithKey);


$.ajax({

    contentType: 'application/json; charset=utf-8',

    type: 'POST',

    url: '/Products/AppendClientFilter',

    data: things,

    success: function (res) {

        console.log('Successs', res);

    },

    failure: function (response) {

        console.log('Error', response);

    }

});

您還可以刪除dataTypein ajax 調用。jQuery ajax 將從響應標頭中猜測正確的類型,在您的情況下,您將顯式返回 JSON。


查看完整回答
反對 回復 2022-01-09
  • 2 回答
  • 0 關注
  • 179 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號