我需要实现上传文件并保存与 orderItemID(我可以获得)相关的功能。问题不是获取 ID 或使用 ID 将文件保存到数据库。问题是将这个 ID(我可以注销并看到它在那里)传递到 Controller 中然后使用,参数继续返回 NULL,当它不是时。
我最初尝试在上传文档时将 orderItemID 作为第二个参数传递,但这导致 HttpPostedFileBase 和 orderItemID 的整数在我进入该方法后立即返回为 NULL。
我已经尝试通过 ViewBag 传递 orderItemID。功能,但它再次返回为 NULL
我现在正在尝试进行第二个单独的 AJAX 调用,它只是传入一个 int (orderItemID),然后在 Controller 中我可以尝试其他操作,但现在我只想看到参数不返回为 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;
}
预期:orderItemID 将等于 12121
实际:NULL
错误:参数字典包含方法“Void SetCurrentOrderItemID(Int32)”的不可为空类型“System.Int32”的参数“orderItemID”的空条目
最佳答案
AJAX 参数中的“数据”属性应如下所示:
data: "cOrderItemID=" + cOrderItemID
编辑:
删除这一行:
contentType: false,
关于javascript - 如果参数不在 AJAX 调用期间,则包含 NULL 条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57786698/