-1

在这里,我试图将模型映射SliderImage到视图模型SliderImageVM并返回视图模型。

SliderImage模型中有

public string Image { get; set; } 

SliderImageVM视图模型中我有

public IFormFile Photo { get; set; }

现在在将模型转换为视图模型时我有错误说

无法从字符串转换为 Microsft.AspNetCore.Http.IFormFile

下面是我的代码

public SliderImageVM GetSliderBySliderImageVMById(int sliderImageId)
{
    var getSliderImage = uow.Repository<SliderImage>().FindBy(x => x.SliderImageId == sliderImageId).FirstOrDefault();
    SliderImageVM newSliderImgeVM = new SliderImageVM
    {
        SliderImageId = getSliderImage.SliderImageId,

        Photo = getSliderImage.Image, //<------Error in this line
    };
    return newSliderImgeVM;
}
4

1 回答 1

0

无法从字符串转换为 Microsft.AspNetCore.Http.IFormFile

如果您想根据通过检索到Photo的文件路径/名称为视图模型的属性分配有效值,您可以参考以下代码片段。SliderImageVMgetSliderImage.Image

var getSliderImage = uow.Repository<SliderImage>().FindBy(x => x.SliderImageId == sliderImageId).FirstOrDefault();

// get the file path based on getSliderImage.Image

using (var stream = System.IO.File.OpenRead($"{filepath}"))
{
    SliderImageVM newSliderImgeVM = new SliderImageVM
    {
        SliderImageId = getSliderImage.SliderImageId,

        Photo = new FormFile(stream, 0, stream.Length, null, Path.GetFileName(stream.Name))
    };

    // ...
    // code logic here
    // ...
}
于 2020-02-07T06:05:49.813 回答