gpt4 book ai didi

javascript - 使用 [FromForm] 属性时集合始终为 null

转载 作者:行者123 更新时间:2023-11-29 10:26:32 24 4
gpt4 key购买 nike

我正在尝试发布包含 FileCollectionFormData

这是我的模型:

public class Content
{
public string Name { get; set; }
public string Link { get; set; }
}
public class Model
{
public IEnumerable<Content> Contents { get; set; }
public IFormFile File { get; set; }
}

这是我的 Action :

[HttpPost]
public async Task InsertAsync([FromForm]Model dataPost)
{
await _services.Create(dataPost);
}

我在 JavaScript 中的 FormData 是:

const formData = new FormData();
formData.append("File", myFile, "image.jpg")
formData.append("Contents", arrayOfContent)

这是header:

'Content-Type': `multipart/form-data`

"file"工作正常,但“内容”始终为空。

我哪里错了?谢谢!

最佳答案

为了测试,我使用了以下 arrayOfContent:

var arrayOfContent = [];
arrayOfContent.push({ name: 'test', link: 'test.com' });
arrayOfContent.push({ name: 'test2', link: 'test2.com' });

我使用 for 循环将数组附加到表单数据:

for (var i = 0; i < arrayOfContent.length; i++) {
formData.append("Contents[" + i + "].Name", arrayOfContent[i].name);
formData.append("Contents[" + i + "].Link", arrayOfContent[i].link);
}

我在 Visual Studio 中看到它可以绑定(bind)它:

enter image description here

关于javascript - 使用 [FromForm] 属性时集合始终为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57745186/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com