Ajax发布始终将空对象返回给控制器

时间:2019-07-08 16:59:22

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

我正在尝试在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("");
            }
        });
    });

我在浏览器中检查了帖子调用,并填充了对象:

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("");
                }
            });
        });

我检查并在帖子中发送对象:

enter image description here

但是JsonResult仍然收到空的InventaryAjax

enter image description here

[更新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("");
                }
            });

2 个答案:

答案 0 :(得分:0)

更改此行

 data: JSON.stringify(inventary),

data: {inventary : inventary } ,

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

我正在使用您的代码,我正在获取所有值

See here

答案 1 :(得分:0)

这是我的建议,

仍然使用下面的行

 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。