gpt4 book ai didi

c# - 如何发布包含 Iformfile 的 View 模型数组的表单?

转载 作者:行者123 更新时间:2023-12-05 07:23:18 25 4
gpt4 key购买 nike

我正在尝试发布 PersonViewModel 列表,只要 PersonViewModel 没有任何 IFormFile 属性,它就可以正常工作。

Razor 页面模型:

public class TestModel : PageModel
{
[BindProperty]
public PersonViewModel[] People { get; set; }

public void OnGet()
{

}

public void OnPost()
{
// Do something with People property
}
}

View 模型:

public class PersonViewModel
{
public string Name { get; set; }
public string LastName { get; set; }
}

HTML 表单:

<form method="post" enctype="multipart/form-data">

<input type="text" name="People[0].Name" class="form-control" />
<input type="text" name="People[0].LastName" class="form-control" />

<input type="text" name="People[1].Name" class="form-control" />
<input type="text" name="People[1].LastName" class="form-control" />

<button type="submit" class="btn btn-success">Submit</button>
</form>

但是,如果我将 IFormFile 添加到 ViewModel 并再次发布表单,表单将不会到达发布操作方法。

View 模型:

public class PersonViewModel
{
public string Name { get; set; }
public string LastName { get; set; }
public IFormFile Photo { get; set; } = null;
}

HTML 表单:

<form method="post" enctype="multipart/form-data">
<input type="text" name="People[0].Name" class="form-control" />
<input type="text" name="People[0].LastName" class="form-control" />
<input type="file" name="People[0].Photo" class="form-control" />

<input type="text" name="People[1].Name" class="form-control" />
<input type="text" name="People[1].LastName" class="form-control" />
<input type="file" name="People[1].Photo" class="form-control" />

<button type="submit" class="btn btn-success">Submit</button>
</form>

项目版本:ASP.NET Core 2.2

项目模板:ASP.NET Razor Pages

是我做错了什么还是与 asp.net core 相关?

最佳答案

我通过将文件绑定(bind)到 List<IFormFile> 找到了解决方法并将值赋给 PhotoPeople在 foreach 循环中。

我还发现它仅在模型绑定(bind)忽略 Photo 时有效属性使用[BindNever]属性。

引用我下面的演示:

View 模型:

public class PersonViewModel
{
public string Name { get; set; }
public string LastName { get; set; }
[BindNever]
public IFormFile Photo { get; set; }
}

页面模型:

public class TestModel : PageModel
{

[BindProperty]
public List<PersonViewModel> People { get; set; }

[BindProperty]
public List<IFormFile> FileLists { get; set; }

public void OnGet()
{

}

public void OnPost()
{
for (int i = 0; i < People.Count; i++)
{
People[i].Photo = FileLists[i];
}
//var formFile = HttpContext.Request.Form.Files;
// Do something with People property
}
}

查看:

<form method="post" id="typea" enctype="multipart/form-data">
<input type="text" name="People[0].Name" class="form-control" />
<input type="text" name="People[0].LastName" class="form-control" />
<input type="file" asp-for="FileLists" class="form-control" />


<input type="text" name="People[1].Name" class="form-control" />
<input type="text" name="People[1].LastName" class="form-control" />
<input type="file" asp-for="FileLists" class="form-control" />

<button class="btn btn-success" type="submit">Submit</button>
</form>

关于c# - 如何发布包含 Iformfile 的 View 模型数组的表单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56065763/

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