大家好日子,
我正在尝试将我的ViewModel从JS传递到我的ASP.NET核心控制器,但我面临的错误代码为500.
以下是我的代码:
我的Javascript
const myFiles = new FormData(); // I supply this myFiles from my append.
// supposed myFiles has already files inside so don't worry about this.
const myInfo = {
Id: 0
Name: "Hello World"
}
const vm = {
MyInfo: myInfo,
MyFiles: myFiles
}
axios.post(`/Info/UploadInfo`,
vm,
{
headers:{
"Content-Type": "multipart/form-data"
}
});
我的ViewModel
public class MyInfoVm{
public MyInfo MyInfo {get;set;}
public IFormCollection MyFiles {get;set;}
}
我的控制器
public IActionResult UploadInfo(MyInfoVm vm){
return Ok();
}
如果我删除VM并且仅使用ViewModel中的一个字段(IFormCollection或MyInfo),这是有效的。但问题是,我需要在单个请求中发送多个参数。有什么帮助吗?
答案 0 :(得分:1)
的Javascript
var fileForm = document.createElement('form');
fileForm.enctype = 'multipart/form-data';
var id = document.createElement('input');
id.name = 'id';
id.value = '0';
fileForm.appendChild(id);
var name = document.createElement('input');
name.name = 'id';
name.value = '0';
fileForm.appendChild(name);
var fileInput = document.createElement('input');
fileForm.appendChild(fileInput);
fileInput.id = 'file-input';
fileInput.type = 'file';
fileInput.name = 'file';
var data = new FormData(fileForm);