如何使用 Asp.net 核心在单个模型中传递 FormFile 和 json 对象
How to pass FormFile and json object in single model using Asp.net core
我有一个模型class。 class 包含多个属性、自定义列表 class 和表单文件数据列表。我需要使用 Asp.net 核心为此模型创建 post 请求。
型号Class
public class TenoModel
{
[Key]
public int Id { get; set; }
public DateTime Date { get; set; }
public float Salary { get; set; }
[Required]
public string User { get; set; }
public List<IformFile> files {get;set;}
public List<Employee> employees{get;set;}
}
为员工
自定义class
public class Employee
{
public int Id {get;set;}
public string Name {get;set;}
public double Salary {get;set;}
}
HttpPost 请求:
[HttpPost("CreateTeno")]
[Consumes('multipart/form-data']
public async Task<IActionResult> CreateTeno([FormFile] TenoModel model)
{
..... save the model data to teno database
}
当我对 Swagger Formfile 数据执行 HttpPost 请求时,一些 属性 数据进入 CreateTeno 方法的传入请求。我也大摇大摆地填充了 Employee 对象。但是这些数据没有出现在我的 CreateTeno 方法中。员工数据列表为零。如何在 HTTP post 请求中传递具有多个属性、IformFile 列表和多个自定义列表 class 的模型 class。举个例子。
您必须像这样更改控制器方法的签名:
public async Task<IActionResult> CreateTeno([FromForm] TenoModel model)
像这样用 Postman 测试过
请注意这里有语法错误:[Consumes('multipart/form-data']
,应该是[Consumes("multipart/form-data")]
我有一个模型class。 class 包含多个属性、自定义列表 class 和表单文件数据列表。我需要使用 Asp.net 核心为此模型创建 post 请求。
型号Class
public class TenoModel
{
[Key]
public int Id { get; set; }
public DateTime Date { get; set; }
public float Salary { get; set; }
[Required]
public string User { get; set; }
public List<IformFile> files {get;set;}
public List<Employee> employees{get;set;}
}
为员工
自定义classpublic class Employee
{
public int Id {get;set;}
public string Name {get;set;}
public double Salary {get;set;}
}
HttpPost 请求:
[HttpPost("CreateTeno")]
[Consumes('multipart/form-data']
public async Task<IActionResult> CreateTeno([FormFile] TenoModel model)
{
..... save the model data to teno database
}
当我对 Swagger Formfile 数据执行 HttpPost 请求时,一些 属性 数据进入 CreateTeno 方法的传入请求。我也大摇大摆地填充了 Employee 对象。但是这些数据没有出现在我的 CreateTeno 方法中。员工数据列表为零。如何在 HTTP post 请求中传递具有多个属性、IformFile 列表和多个自定义列表 class 的模型 class。举个例子。
您必须像这样更改控制器方法的签名:
public async Task<IActionResult> CreateTeno([FromForm] TenoModel model)
像这样用 Postman 测试过
请注意这里有语法错误:[Consumes('multipart/form-data']
,应该是[Consumes("multipart/form-data")]