简体   繁体   English

当不在AJAX调用过程中时,该参数包含NULL条目

[英]Parameter contains a NULL entry when it isn't during AJAX call

I need to implement functionality to upload files and save them relevant to the orderItemID (which I can get). 我需要实现上传文件并保存与orderItemID相关的文件的功能(可以获取)。 The problem isn't getting the ID or using the ID to then save the files to the DB. 问题不在于获取ID或使用ID然后将文件保存到DB。 The problem is passing this ID (which I can log out and see is there) into the controller to then be used, the parameter continues to come back NULL when it isn't. 问题是将此ID(我可以注销并看到该ID)传递到控制器中,然后使用该参数,否则该参数将继续返回NULL。

I initially tried passing the orderItemID as a second parameter when uploading the document but that resulted in both the HttpPostedFileBase and the int of orderItemID coming back as NULL as soon as I entered the method. 我最初尝试在上传文档时将orderItemID作为第二个参数传递,但是一旦我进入方法,就会导致HttpPostedFileBase和orderItemID的int都返回为NULL。

I've tried passing the orderItemID through the ViewBag. 我尝试过通过ViewBag传递orderItemID。 feature but again it comes back as NULL 功能,但再次返回为NULL

I'm now trying to make a second separate AJAX call which simply passes in an int (orderItemID) and then in the controller I can try other things but for now I'd just like to see the parameter not returning as NULL when I hit the breakpoint in the orderController. 我现在正在尝试进行第二个单独的AJAX调用,该调用仅传递一个int(orderItemID),然后在控制器中我可以尝试其他操作,但是现在我只想看看当我点击时参数不返回为NULL orderController中的断点。

View:
        $('#confirmUploadDiagrambtn').on("click", function () {
            var currentID = document.getElementsByName("orderitemid")[0].value;
            var form = $("#file")[0].files[0];
            var datastring = new FormData();
            datastring.append('file', form);

            //Order Item ID is logged out as the expected 12121
            console.log("OrderID: " + currentID);

            //Errors out saying parameter is NULL
            setOrderItemID(currentID);

            uploadDocument(datastring);
        })

        function setOrderItemID(cOrderItemID) {
            $.ajax({
                type: "POST",
                url: '/Order/SetCurrentOrderItemID',
                data: cOrderItemID,
                contentType: false,
                processData: false,
                success: function (response) {
                    console.log('Item Success');
                    console.log(response);
                },
                error: function (status) {
                    console.log('Item Error');
                    console.log(status);
                }
            })
        }

Controller:

        [HttpPost]
        public void SetCurrentOrderItemID(int orderItemID)
        {
            //I can try whatever later, just need the param to not be NULL
            ViewBag.cOrderItemID = orderItemID;
        }

Expected: orderItemID will equal 12121 预期:orderItemID将等于12121

Actual: NULL 实际:NULL

Error: The parameters dictionary contains a null entry for parameter 'orderItemID' of non-nullable type 'System.Int32' for method 'Void SetCurrentOrderItemID(Int32)' 错误:参数字典包含方法'Void SetCurrentOrderItemID(Int32)'的非空类型'System.Int32'的参数'orderItemID'的空条目

the "data" property in the AJAX parameters should look like: AJAX参数中的“数据”属性应如下所示:

data: "cOrderItemID=" + cOrderItemID

EDIT: 编辑:

remove this line: 删除此行:

contentType: false,

使用以下格式:

data : {OrderItemID : cOrderItemID}

By default int parameters are assumed to come from the query string, you could either change the url to: 默认情况下,假定int参数来自查询字符串,您可以将url更改为:

function setOrderItemID(cOrderItemID) {
        $.ajax({
            type: "POST",
            url: '/Order/SetCurrentOrderItemID?orderItemID=' + cOrderItemID,
            contentType: false,
            processData: false,
            success: function (response) {
                console.log('Item Success');
                console.log(response);
            },
            error: function (status) {
                console.log('Item Error');
                console.log(status);
            }
        })
    }

Or else you could mark the parameter with the [FromBody] attribute: 否则,您可以使用[FromBody]属性标记该参数:

[HttpPost]
public void SetCurrentOrderItemID([FromBody] int orderItemID)
{
    //I can try whatever later, just need the param to not be NULL
    ViewBag.cOrderItemID = orderItemID;
}

and then update your data parameter to: 然后将您的数据参数更新为:

data: JSON.stringify({orderItemId: cOrderItemID}),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM