Javascript 使用Axios[Asp]将图像文件传递到ViewModel时出现问题
在我的应用程序中,我希望传递图像文件和其他参数,并将其传递给ViewModel。 我可以通过以下方式仅发送图像文件:Javascript 使用Axios[Asp]将图像文件传递到ViewModel时出现问题,javascript,asp.net,axios,Javascript,Asp.net,Axios,在我的应用程序中,我希望传递图像文件和其他参数,并将其传递给ViewModel。 我可以通过以下方式仅发送图像文件: axios.post(url, formData, headers: { 'Content-Type': 'multipart/form-data' }) .then(response => { console.log(response) }) .catch(error => { cons
axios.post(url,
formData,
headers: { 'Content-Type': 'multipart/form-data' })
.then(response => {
console.log(response)
})
.catch(error => {
console.log(error)
})
这样接受它:
[HttpPost]
[Route("some/url")]
public JsonResult Something(IFormCollection vm)
{
Console.WriteLine(vm);
return Json(vm);
}
但我希望传递多个参数,而不仅仅是FormFiles,因此我实现了一个ViewModel来接受多个值:
public class ViewModelSomething : ViewModelBase
{
public string Typ { get; set; }
public IFormCollection Image { get; set; }
}
如果我现在使用axios发送数据:
axios({
method: 'post',
url: url,
data: {
"Typ": someData,
"Images": formData
},
headers: { 'Content-Type': 'multipart/form-data'}
})
.then(response => {
console.log(response)
})
.catch(error => {
console.log(error)
})
致我的控制器:
public JsonResult Something(ViewModelSomething vm){ Console.WriteLine(vm)...}
我所有的值都是空的。我认为这是因为Axios中的标题,但是如果我的ViewModel中只有属性图像,同样的问题也会发生。
我感谢每一个暗示
编辑:我也尝试序列化它,但这也没有什么不同