Search code examples
javascriptc#htmlajaxviewbag

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). The problem isn't getting the ID or using the ID to then save the files to the 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.

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.

I've tried passing the orderItemID through the ViewBag. feature but again it comes back as 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.

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

Actual: NULL

Error: The parameters dictionary contains a null entry for parameter 'orderItemID' of non-nullable type 'System.Int32' for method 'Void SetCurrentOrderItemID(Int32)'


Solution

  • By default int parameters are assumed to come from the query string, you could either change the url to:

    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:

    [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}),