简体   繁体   English

如何将 object 列表与 Angular 表单数据发布到.Net Core Web ZDB974238714831DE634ZA7CED1

[英]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.

相关问题 如何使用mailkit将联系表单数据发送到API(Angular 7 + .Net core web api) - How to send contact form-data to API using mailkit (Angular 7 + .Net core web api) Angular /.NET Core API - System.InvalidOperationException:'不正确的内容类型:应用程序/表单数据' - Angular /.NET Core API - System.InvalidOperationException: 'Incorrect Content-Type: application/form-data' 如何将 Angular 8 中的字符串值发布到 .NET 核心 Web ZDB974238714CA8DE634A7CE1DZF08A? - How to post a string value from Angular 8 to .NET core Web API? 如何从角度发布多部分/表单数据到Django REST API - How to post multipart/form-data from angular to Django REST API 如何使用angular2中的form-data发送帖子 - How send post with form-data in angular2 如何使用 POST 方法在 Angular 中发送表单数据? - How to use POST method to send form-data in Angular? 如何将多部分/表单数据从 Angular 发布到 Nodejs Multer? - How to post multipart/form-data from Angular to Nodejs Multer? 如何使用.NET Core中的Angular 4在同一提交中发布表单数据和文件 - How to post Form data and File in the same submit with Angular 4 in .NET Core 使用 Angular 9 在 Asp.Net Core Web API 中发布数据(发布请求)时出错 - Error while posting data (Post request) in Asp.Net Core Web API with Angular 9 Angular 2 http发布到.Net Core Web Api 500错误 - Angular 2 http post to .Net Core Web Api 500 error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM