AJAX POST正在将NULL数据返回到控制器 [英] AJAX POST is returning NULL data to the controller

查看:65
本文介绍了AJAX POST正在将NULL数据返回到控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我有一个AJAX POST会将空数据返回到我的控制器.

Hello I have an AJAX POST that is returning null data to my Controller.

这是我的AJAX代码

$(document).ready(function () {
    $("button").click(function () {
        $.ajax({
            url: '@IGT.baseUrl/JODetails/SpecialOrderSummary',
            data: $('#form').serialize(),
            type: 'POST'
        });
    });
});

这是我的控制器

public ActionResult SpecialOrderSummary(ItemViewModel model)
{
    if (model == null)
    {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }

    JobOrder jobOrder = db.JobOrders.Find(model.Id);
    if (jobOrder == null)
    {
        return HttpNotFound();
    }

    return View(model);
}

这是我的ItemViewModel

Here is my ItemViewModel

public class ItemViewModel
{
    [Required]
    public int Id { get; set; }
    [Required]
    public int JobId { get; set; }
    public string ItemId { get; set; }
    public string ItemName { get; set; }
    public string MFGNumber { get; set; }
    public IList<ItemPartViewModel> Parts { get; set; }
    public IList<ItemComponentViewModel> Components{ get; set; }
    public IList<ComponentPartViewModel> ComponentParts { get; set; }
    public IList<ComponentSubCompViewModel> ComponentSubComps { get; set; }
    public IList<SubCompPartViewModel> SubCompParts { get; set; }

    public IList<SubCompSubCompViewModel> SubCompSubComps { get; set; }
    public IList<SubCompSubCompPartViewModel> SubCompSubCompParts { get; set; }
}

它正在返回模型,但是模型中有空数据,为什么会这样?

It's returning the model but the the model has empty data, why is this?

我最初做的是普通的POST,但该帖子缺少一些关键数据,如您在

I had originally done a normal POST but the post was missing some key data as you can see in Unexpected nulls in ViewModel on Form Post

推荐答案

进行AJAX调用时,您需要传递JSON对象.

You need to pass a JSON object when you make the AJAX call.

我通常使用一个数组,其中您在 ItemViewModel 中使用了 IList< T> –我认为这只是使序列化过程更简单.

I typically use an array, where you have used IList<T> in your ItemViewModel -- I think it just makes the serialization story simpler.

以下是一些示例代码,可以帮助您入门.

Here is some sample code to get you started.

$(document).ready(function () {
    $("button").click(function () {

        // build the json object from the elements in the form... 

        // this is a sample object that would work well with your current model... 
        var payload = {
            "Id": 5,
            "JobId": 6,
            "ItemId": "Item0987", 
            "ItemName": "Some Sample Item Name",
            "MFGNumber": "B1235456",
            "Parts": [
                { "Id": "PartA", "PartName": "Sample Part A" },
                { "Id": "PartB", "PartName": "Sample Part B" }
            ]
        };

        $.ajax({
            url: "@IGT.baseUrl/JODetails/SpecialOrderSummary",
            data: JSON.stringify(payload),
            contentType: "application/json; charset=utf-8",            
            type: "POST", 
            success: function (data) {
                console.log(data);
            }, 
            error: function (req, status, err) {
                console.error(err);
            },
            statusCode: {
                404: function() {
                   console.error( "page not found" );
                },
                500: function() {
                   console.error( "server error" );
                },
            }
        });
    });
});

我在 $.ajax 调用中添加了一些错误处理,以帮助您捕获除表格序列化之外的其他问题.

I added some error handling to the $.ajax call to help you catch other issues besides the form serialization.

这篇关于AJAX POST正在将NULL数据返回到控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆