如何在一个 POST 请求中使用 IFormFileCollection 发送 int



我正在尝试上传图像(我正在使用IFormFileCollection(,但还需要在同一个 POST 请求中获取额外的数据(简单的 int 值(。

我的控制器如下所示:

public async Task<ActionResult<List<Guid>>> Create(IFormFileCollection uploads, int id) { }

我用来测试它的 cshtml 看起来像这样:

<form asp-action="Create" asp-controller="ProjectMedia" method="post" enctype="multipart/form-data">
    <input type="file" name="uploads" /><br>
    <input type="file" name="uploads" /><br>
    <input type="number" name="id" /><br>
    <input type="submit" value="Upload" />
</form>

我在接收要上传的文件方面没有任何问题,但是除了它们,我什么也得不到。 id 始终默认为 0。我也尝试使用 asp-route 设置,但在控制器上得到了相同的 0。是否有机会在一个请求中发送 IFormFileCollection 和 int?如果是这样,我该怎么做?

使用[FromForm]属性显式告知模型绑定器从何处获取操作的数据

public async Task<ActionResult<List<Guid>>> Create([FromForm]IFormFileCollection uploads, [FromForm]int id) { 
}

ASP.NET 核中的参考模型绑定

最新更新