【问题标题】:Issue while converting IFormFile to string将 IFormFile 转换为字符串时出现问题
【发布时间】: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


【解决方案1】:

首先你不需要这个DatabaseGenerated(DatabaseGeneratedOption.Identity) 因为默认是尝试从我的代码中理解

public class MParent
    {
        [Key]
        public int Id { get; set; }
        public List<child> Children { get; set; }
    }
    public class Mchild
    {
        [Key]
        public int Id { get; set; }
        public string VideoId { get; set; }   
        public int ParentId { get; set; }
    }

查看模型

 public class Parent
    {
        
        public int Id { get; set; }
        public List<child> Children { get; set; }
    }
    public class child
    {
        
        public int Id { get; set; }
        public IFormFile VideoId { get; set; } 
        public int ParentId { get; set; }
    }

和我的控制器

 [HttpPost]
    public IActionResult Create(child child, IFormFile myfile)
    {

        Mchild mychild = new Mchild()
        {
            ParentId = child.ParentId,
            VideoId = myfile.FileName
        };
        myfile.CopyToAsync(myfileLoctionTosaveVideo);
        _context.add(mychild);
        _context.savechanges();

        return View();
     }

你可以用文件来做这些

【讨论】:

    猜你喜欢
    • 2020-03-17
    • 1970-01-01
    • 2022-01-08
    • 1970-01-01
    • 1970-01-01
    • 2022-11-11
    • 2014-09-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多