[英]How to POST list of object with Angular form-data to .Net Core Web API
I want to post a list of object to.Net core web API from angular 9 application, Here i am using form-data because i need to post image with data and this is working for me but now a list of object property added to my view model. I want to post a list of object to.Net core web API from angular 9 application, Here i am using form-data because i need to post image with data and this is working for me but now a list of object property added to my查看 model。 Here is my code example:这是我的代码示例:
// ViewModel
public class ViewModel
{
public string Name { get; set; }
public IFormFile Image { get; set; }
public List<Connect> Connects { get; set; }
}
public class Connect
{
public string Name { get; set; }
public string Link { get; set; }
}
// .Net Core Action
[HttpPost]
public async Task<IActionResult> Post([FromForm] ViewModel vm)
{
}
// Angular component.ts
onSubmit() {
const formData = new FormData();
formData.append('name', this.model.name);
formData.append('image', this.form.get('image').value);
// Want add a list of object here to post with this
formData.append('connects', this.model.connects);
}
// Angular component.ts
onSubmit() {
const formData = new FormData();
formData.append('name', this.model.name);
for (let i = 0; i < this.model.connects.length; i++) {
if (this.model.connects[i].name !== '' && this.model.connects[i].link !== '') {
formData.append('connects[' + i + '][name]', this.model.connects[i].name);
formData.append('connects[' + i + '][link]', this.model.connects[i].link);
}
}
// Rest of the code
}
Like Example像例子
In order to allow ASP.NET
Core model binder to bind an array you need to add array values multiple times with the same parameter name为了允许ASP.NET
Core model binder 绑定数组,您需要使用相同的参数名称多次添加数组值
For Eg:-例如:-
let formData: FormData = new FormData();
formData.append("name", "name");
for(let i = 0; i < Ids.length; i++) {
formData.append("Ids", Ids[i]);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.