【问题标题】:Getting empty list when passing data through ajax in asp.net core在asp.net核心中通过ajax传递数据时获取空列表
【发布时间】:2021-04-24 18:28:36
【问题描述】:

我通过 ajax 将列表数据从视图发送到控制器,数据显示正确,但在控制器中接收到空的对象列表。这是我的代码 sn-p。

这是我的 java 脚本代码:-

//Collect Multiple List For Pass To Controller
    $("#btnSubmit").click(function (e) {
        e.preventDefault();
       // NavContinue();

        var _data = [];
        $.each($("#gridTable tbody tr"), function () {
            _data.push({
                ItemDescription: $(this).find('td:eq(1)').find('input').val(),
                ItemCode: $(this).find('td:eq(2)').find('select').val(),
                Quantity: $(this).find('td:eq(3)').find('input').val(),
                
            });
        });

        var data = JSON.stringify({
            abc : _data
        });
        console.log(data);
        $.when(Save(data)).then(function (response) {
            console.log(response);
        }).fail(function (err) {
            console.log(err);
        });
    });

    //After Click Save Button Pass All Data View To Controller For Save Database
    function Save(abc) {
        alert(abc);
        return $.ajax({
             contentType: 'application/json; charset=utf-8',
            //contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
            dataType: 'json',
            type: 'POST',
            url: "/controller/save",
            data: abc,
            success: function (result) {
                if (result) {
                    alert(result);
                }
                else {
                    location.reload();
                }
            }
        });
    }

这是我的模型:-

 public class abc
    {
        public string ItemDescription { get; set; }
        public int ItemCode { get; set; }
        public int Quantity { get; set; }

    }

这是我的控制器:-

 [HttpPost]
    public JsonResult save(abc[] model)
    {
        var response = model;
        return Json(true);
    }

如果有人帮助我,将不胜感激

【问题讨论】:

标签: c# jquery


【解决方案1】:

要让模型绑定器处理您在 AJAX 请求中发送的数据,您需要将数组放在您发送的 JSON 字符串的根目录中,而不是在 abc 属性中。试试这个:

var data = JSON.stringify(_data);

【讨论】:

  • 还是一样的响应,是有线的。
猜你喜欢
  • 2021-03-19
  • 1970-01-01
  • 2019-11-29
  • 2021-09-27
  • 2015-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-13
相关资源
最近更新 更多