[英]How to put an IFormFile into a View Model in MVC6
我正在嘗試創建一個帶有圖像上傳字段和名稱字段的表單,並使用視圖模型進行驗證(參見下文)。 問題是當我將我的數據庫遷移到此類的帳戶或在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; }
}
}
我錯過了什么?
事實證明代碼沒有任何問題,我只是在創建視圖時意外選擇了一個Data Context Class
,這意味着EF
開始關注視圖模型,它絕對不應該這樣(View Models不應該靠近數據庫)。
此外,您可以像這樣從modelView驗證文件擴展名
[FileExtensions(Extensions="jpg,jpeg,png,pdf")]
public IFormFile ImageUpload {get; set;}
如何從模型視圖驗證文件大小,請問任何一個?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.