简体   繁体   中英

How to put an IFormFile into a View Model in MVC6

I'm trying to create a form with an image upload field and name field, validated with a view model (see below). Problem is when I migrate my database to account for this class or hit play in Visual Studio (starting the site), I get the following error:

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)

Code:

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; }
    }
}

What am I missing?

事实证明代码没有任何问题,我只是在创建视图时意外选择了一个Data Context Class ,这意味着EF开始关注视图模型,它绝对不应该这样(View Models不应该靠近数据库)。

In addition you can validation file extension from the modelView like this

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

How about how to validate file size from the model view, any one please?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM