【发布时间】:2021-12-05 12:39:57
【问题描述】:
我正在做一个 asp.netcore 3.1 项目。我有 2 个具有一对多关系的表。
我在将 VideoId 从 IFormFile 转换为 String 时遇到问题。
这是我的模型和视图模型。
ParentModel.cs
public class Parent
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public List<child> Children { get; set; }
}
public class child
{
public int Id { get; set; }
public string VideoId { get; set; } // inside ParentModel videoId is string type
public int ParentId { get; set; }
}
ParentViewModel.cs
public class Parent
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public List<child> Children { get; set; }
}
public class child
{
public int Id { get; set; }
public IFormFile VideoId { get; set; } // inside ParentViewModel VideoId is IFormFile type
public int ParentId { get; set; }
}
ParentController.cs
public async Task<IActionResult> Create([FromForm] ParentViewModel parent)
{
var result = await _ParentService.Create(_mapper.Map<ParentModel>(parent)); // this line is ok. After this line VideoId is Microsoft.AspNetCore.Http.FormFile
if (parent.Child.Count > 0)
{
int id = 0;
foreach (var child in parent.Child){
var videoIdString = await _helper.UploadImage(child.VideoId, "path"); // In this line I convert videoId of child of ParentViewModel to string from IFormFile. Now VideoId is path.mp4
}
id++;
}
// I have to update VideoId of child of ParentModel with that coverted videoIdString
await _ParentService.Update(result);
return Created(nameof(Get), result);
}
我必须用转换后的videoIdString 更新VideoId of the child of ParentModel
我该怎么做?大家有什么想法,请...
【问题讨论】:
-
您的表单必须具有 enctype="multipart/form-data" 才能将数据发送到控制器检查文件是否来自表单
-
不,这不是我的问题,我的问题是如何映射 result.Child.VideoId 和 parent.Child.VideoId
标签: entity-framework asp.net-core mapping asp.net-core-webapi