【问题标题】:Ajax post return empty object always to controllerAjax post 总是将空对象返回给控制器
【发布时间】:2019-07-08 16:59:22
【问题描述】:

我正在尝试在我的 ASP.NET MVC 应用程序中使用 Ajax 发布帖子,但对象始终为空。

这是我的控制器操作:

[HttpPost]
public JsonResult Newinventary(InventaryAjax inventary)
{
    return Json(inventary);
}

这在我的班级 InventaryAjax 中:

    public string SerialNumber { get; set; }
    public decimal SupplierCode { get; set; }
    public string WithdrawalOrder { get; set; }
    public string EnterpriseDocument { get; set; }
    public string Description { get; set; }
    public int LerRaee { get; set; }
    public string Weigth { get; set; }
    public decimal Price { get; set; }
    public string GeneralComments { get; set; }
    public string HdserialsNumbers { get; set; }

这是我的电话:

$("body").on("click", "#btnAdd", function () {
        var inventary = new Object();
        inventary.SupplierCode = $("#txtSupplier").val();
        inventary.WithdrawalOrder = $("#txtOrden").val();
        inventary.EnterpriseDocument = $("#txtDocumento").val();
        inventary.LerRaee = $("#txtLER").val();
        inventary.Weigth = $("#txtPeso").val();
        inventary.Description = $("#txtDescripcion").val();
        inventary.SerialNumber = $("#txtSN").val();
        inventary.HdserialsNumbers = $("#txtSNHDD").val();
        inventary.Price = $("#txtPrecio").val();
        inventary.GeneralComments = $("#txtObservaciones").val();
        $.ajax({
            type: "POST",
            url: "/Almacen/NewInventary",
            data: JSON.stringify(inventary),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (r) {
                var row = $("#tblInventary tr:last-child");
                if ($("#tblInventary tr:last-child span").eq(0).html() != " ") {
                    row = row.clone();
                }
                AppendRow(row, r.SupplierCode,
                    r.WithdrawalOrder, r.EnterpriseDocument,
                    r.LerRaee, r.Weigth, r.Description,
                    r.SerialNumber, r.HdserialsNumbers,
                    r.Price, r.GeneralComments);
                txtLER.val("");
                txtPeso.val("");
                txtDescripcion.val("");
                txtSN.val("");
                txtSNHDD.val("");
                txtPrecio.val("");
                txtObservaciones.val("");
            }
        });
    });

我在浏览器中查看了post调用,对象被填充了:

Description: "5"
EnterpriseDocument: "2"
GeneralComments: "9"
HdserialsNumbers: "7"
LerRaee: "3"
Price: "8"
SerialNumber: "6"
SupplierCode: "0"
Weigth: "4"
WithdrawalOrder: "1"

但是当我签入方法时,控制器中接收到的对象都是空的

[更新] 我尝试更改 ajax 帖子的数据:

 $("body").on("click", "#btnAdd", function () {
            var inventary = new Object();
            inventary.SupplierCode = $("#txtSupplier").val();
            inventary.WithdrawalOrder = $("#txtOrden").val();
            inventary.EnterpriseDocument = $("#txtDocumento").val();
            inventary.LerRaee = $("#txtLER").val();
            inventary.Weigth = $("#txtPeso").val();
            inventary.Description = $("#txtDescripcion").val();
            inventary.SerialNumber = $("#txtSN").val();
            inventary.HdserialsNumbers = $("#txtSNHDD").val();
            inventary.Price = $("#txtPrecio").val();
            inventary.GeneralComments = $("#txtObservaciones").val();
            $.ajax({
                type: "POST",
                url: "/Almacen/NewInventary",
                data: {inventary : inventary },
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (r) {
                    var row = $("#tblInventary tr:last-child");
                    if ($("#tblInventary tr:last-child span").eq(0).html() != " ") {
                        row = row.clone();
                    }
                    AppendRow(row, r.SupplierCode,
                        r.WithdrawalOrder, r.EnterpriseDocument,
                        r.LerRaee, r.Weigth, r.Description,
                        r.SerialNumber, r.HdserialsNumbers,
                        r.Price, r.GeneralComments);
                    txtLER.val("");
                    txtPeso.val("");
                    txtDescripcion.val("");
                    txtSN.val("");
                    txtSNHDD.val("");
                    txtPrecio.val("");
                    txtObservaciones.val("");
                }
            });
        });

我检查并在邮件中发送对象:

但是 JsonResult 仍然收到一个空的 inventaryAjax

[更新 2]

我只更改发送到 json 的数据和控制器方法中接收的数据类型

public ActionResult InsertInventary(string data)
{
    InventaryAjax inventary = JsonConvert.DeserializeObject<InventaryAjax>(data);
    return Json(inventary);
}

$.ajax({
                type: "POST",
                url: '@Url.Action("InsertInventary","Almacen")',
                data: JSON.stringify(inventary) ,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (r) {
                    var row = $("#tblInventary tr:last-child");
                    if ($("#tblInventary tr:last-child span").eq(0).html() != "&nbsp;") {
                        row = row.clone();
                    }
                    AppendRow(row, r.SupplierCode,
                        r.WithdrawalOrder, r.EnterpriseDocument,
                        r.LerRaee, r.Weigth, r.Description,
                        r.SerialNumber, r.HdserialsNumbers,
                        r.Price, r.GeneralComments);
                    txtLER.val("");
                    txtPeso.val("");
                    txtDescripcion.val("");
                    txtSN.val("");
                    txtSNHDD.val("");
                    txtPrecio.val("");
                    txtObservaciones.val("");
                }
            });

【问题讨论】:

  • 您是否尝试发送对象本身? data: inventary
  • 是的,我试过了,但好像不行

标签: ajax asp.net-mvc post model-view-controller


【解决方案1】:

改变这一行

 data: JSON.stringify(inventary),

data: {inventary : inventary } ,

你不需要对对象进行字符串化

我正在使用你的代码,我得到了所有的值

【讨论】:

  • O 已经尝试过了,我看到该对象已发送:SupplierCode=1&WithdrawalOrder=2&EnterpriseDocument=3&LerRaee=4&Weigth=5&Description=6&SerialNumber=7&HdserialsNumbers=8&Price=9&GeneralComments=0。但我仍然在 JsonResult 中收到空
  • @FranciscoGonzalez 尝试更新答案,看看你得到了什么
  • 我不知道问题出在哪里
  • @FranciscoGonzalez 你解决了这个问题吗?我只想知道你用的是哪个newtonsoft版本
  • 不,我无法解决问题,我使用的是 12.0.2 版本
【解决方案2】:

这是我推荐的,

仍然使用下面的行

 data: JSON.stringify(inventary)

但将您的控制器更改为使用JsonConvert 来反序列化对象

[HttpPost]
public JsonResult Newinventary(string data)
{
    InventaryAjax inventary = JsonConvert.DeserializeObject<InventaryAjax>(data);
    return Json(inventary);
}

编辑:

用这个发帖

$.post("/Almacen/NewInventary",
    {
        data: JSON.stringify({
            data : inventory
        })
   },
   function (data, status) {
      console.log(data);
   });

还要在您的控制器中进行调试,以确保 string 数据的值来自 ajax。

【讨论】:

  • 我也试过了,我添加了一个文档写入来查看要发送的帖子数据,看起来是正确的:{"SupplierCode":"1","WithdrawalOrder":"2","EnterpriseDocument ":"3","LerRaee":"4","Weigth":"5","Description":"6","SerialNumber":"7","HdserialsNumbers":"8","价格": "9","GeneralComments":"0"} 不知道问题出在哪里
  • 我用你的解决方案更新了我的问题,但我不知道会发生什么
猜你喜欢
  • 2017-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多