我想从 Angular 9 应用程序将对象列表发布到 .Net Core Web API,这里我使用表单数据,因为我需要发布带有数据的图像,这对我有用,但现在将对象属性列表添加到我的查看模型。这是我的代码示例:
// 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);
}
Run Code Online (Sandbox Code Playgroud)
// 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
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2459 次 |
| 最近记录: |