【问题标题】:How to put an IFormFile into a View Model in MVC6如何将 IFormFile 放入 MVC6 中的视图模型中
【发布时间】:2016-04-12 00:04:02
【问题描述】:

我正在尝试创建一个带有图像上传字段和名称字段的表单,并使用视图模型进行验证(见下文)。问题是当我迁移我的数据库以考虑此类或在 Visual Studio(启动站点)中点击播放时,我收到以下错误:

InvalidOperationException: The property 'ImageUpload' on entity type 'BallWallWebApp.ViewModels.Slides.CreateSlideViewModel' has not been added to the model or ignored.
Microsoft.Data.Entity.Metadata.Conventions.Internal.PropertyMappingValidationConvention.Apply(InternalModelBuilder modelBuilder)

代码:

using Microsoft.AspNet.Http;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;

namespace BallWallWebApp.ViewModels.Slides
{
    public class CreateSlideViewModel
    {
        [Required]
        public int WallId { get; set; }

        [Required]
        [Display(Name = "Slide Name")]
        [DataType(DataType.Text)]
        public string Name { get; set; }

        [Required]
        [Display(Name = "Slide image file")]
        [DataType(DataType.Upload)]
        public IFormFile ImageUpload { get; set; }
    }
}

我错过了什么?

【问题讨论】:

    标签: c# asp.net asp.net-core asp.net-core-mvc


    【解决方案1】:

    结果证明代码没有任何问题,我只是在创建视图时不小心选择了 Data Context Class,这意味着 EF 开始关心它绝对不应该关心的视图模型(视图模型不应该靠近数据库)。

    【讨论】:

      【解决方案2】:

      此外,您可以像这样从模型视图验证文件扩展名

      [FileExtensions(Extensions="jpg,jpeg,png,pdf")]
      public IFormFile ImageUpload {get; set;}
      

      请问如何从模型视图中验证文件大小?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-07-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-04
        • 2016-11-19
        • 2019-09-27
        • 1970-01-01
        相关资源
        最近更新 更多