2017-04-01 15 views
2

である私は、私はファイル(画像)を救うことができるフォームを作成しようとしているが、それは私にエラーを示しています。プロパティは、インターフェイスタイプ(「IFormFile」)MVCコア

と、InvalidOperationException:プロパティ 'Product.Image'はインターフェイスタイプ( 'IFormFile')です。ナビゲーションプロパティの場合は、マッピングされたエンティティタイプにキャストしてこのプロパティの関係を手動で設定します。そうでない場合は、モデルからプロパティを無視します。

Product.cs

public class Product 
    { 
     public Product() 
     { 
      OrderDetails = new HashSet<OrderDetails>(); 
     } 

     public int Id { get; set; } 
     public string Name { get; set; } 
     public string Description { get; set; } 
     public int? CategoryId { get; set; } 
     public decimal? Price { get; set; } 
     public int? Quantity { get; set; } 
     public string ImagePath { get; set; } 

     public virtual ICollection<OrderDetails> OrderDetails { get; set; } 
     public virtual Category Category { get; set; } 
    } 

ProductFormViewModel.cs

public class ProductFormViewModel 
    { 
     public int Id { get; set; } 
     public string Name { get; set; } 
     public string Description { get; set; } 
     public int? CategoryId { get; set; } 
     public decimal? Price { get; set; } 
     public int? Quantity { get; set; } 
     public IFormFile Image { get; set; } 
    } 

[HttpGet] 
      public IActionResult Create() 
      { 
       var categories = _repository.GetCategories().ToList(); 
       var categoriesModel = categories.Select(p => new 
       { 
        p.Id, 
        p.Name 
       }); 

       ViewBag.Categories = new SelectList(categoriesModel, "Id", "Name"); 

       return View(); 
      } 

[HttpPost] 
     public IActionResult Create(ProductFormViewModel product) 
     { 

      var file = product.Image; // **it returns NULL** 
      var upload = Path.Combine(_environment.ContentRootPath, "wwwroot\\uploads", product.Name); 

      if (!Directory.Exists(upload)) 
       Directory.CreateDirectory(upload); 

      var filePath = Path.Combine(upload, file.FileName); 


      if (file.Length > 0) 
      { 
       using (var fileStream = new FileStream(filePath, FileMode.Create)) 
       { 
        file.CopyTo(fileStream); 
       } 
      } 

      var producti = new Product(); 
      producti.CategoryId = product.CategoryId; 
      producti.Description = product.Description; 
      producti.Name = product.Name; 
      producti.Price = product.Price; 
      producti.Quantity = product.Quantity; 
      producti.ImagePath = filePath; 
      _repository.AddProduct(producti); 
      _repository.SaveChanges(); 




      return RedirectToAction("Index","Products"); 
     } 
アクションを作成します。 は、私はここにコードがあります、それを修正する方法を知らない

を適用します

Create.cshtml

@model ProductFormViewModel 
<br /> 
<br /> 
<div class="container"> 
    <div class="panel panel-default"> 
     <div class="panel-heading"> 
     </div> 
     <div class="panel-body"> 
      <form class="form-group" asp-action="Create" asp-controller="Products" method="post"> 
       <input type="hidden" asp-for="Id"/> 
       <div class="col-md-12"> 
        <div class="form-group col-md-6"> 
         <label asp-for="Name" class="control-label col-md-3"></label> 
         <input asp-for="Name" type="text" class="form-control col-md-3"/> 
        </div> 
        <div class="form-group col-md-6"> 
         <label asp-for="CategoryId" class="control-label col-md-3"></label> 
         <select asp-for="CategoryId" asp-items="@ViewBag.Categories" class="form-control col-md-3"> 
          <option hidden disabled selected >Select One</option> 
         </select> 
        </div> 
        <div class="form-group col-md-6"> 
         <label asp-for="Description" class="control-label col-md-3"></label> 
         <textarea asp-for="Description" class="form-control" rows="4"></textarea> 

        </div> 
        <div class="form-group col-md-6"> 

         <label asp-for="Price" class="control-label col-md-3"></label> 
         <input type="text" asp-for="Price" class="form-control col-md-3"/> 
        </div> 
        <div class="form-group col-md-6"> 
         <label asp-for="Quantity" class="control-label col-md-3"></label> 
         <input type="text" asp-for="Quantity" class="form-control col-md-3"/> 
        </div> 
        <div class="form-group col-md-12"> 
         <label class="control-label">Select Image</label> 
         <input asp-for="Image" type="file" class="btn-file"/> 
        </div> 
        <div class="form-group col-md-12 text-center"> 
         <input type="submit" class="btn btn-success" value="Save"/> 
        </div> 
       </div> 
      </form> 
     </div> 
    </div> 
</div> 

答えて

2

IFormFileは、ASP.NETコアフレームワークによって使用されるタイプであり、それは、SQL Serverの型相当するものはありません。

ドメインモデルの場合はbyte[]と保存してください。ビューを操作する場合はIFormFileタイプを使用しても問題ありません。

ProductModel:

public class Product 
{ 
    public Product() 
    { 
     OrderDetails = new HashSet<OrderDetails>(); 
    } 

    public int Id { get; set; } 
    public string Name { get; set; } 
    public string Description { get; set; } 
    public int? CategoryId { get; set; } 
    public decimal? Price { get; set; } 
    public int? Quantity { get; set; } 
    public string ImagePath { get; set; } 

    public virtual ICollection<OrderDetails> OrderDetails { get; set; } 
    public virtual Category Category { get; set; } 
} 

ProductViewModel:

public class ProductViewModel 
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 
    public string Description { get; set; } 
    public int? CategoryId { get; set; } 
    public decimal? Price { get; set; } 
    public int? Quantity { get; set; } 
    public IFormFile Image { get; set; } 
} 

コントローラ方法:

[HttpGet] 
public IActionResult Create() 
{ 
    var categories = _repository.GetCategories().ToList(); 
    var categoriesModel = categories.Select(p => new 
    { 
     p.Id, 
     p.Name 
    }); 

    ViewBag.Categories = new SelectList(categoriesModel, "Id", "Name"); 

    return View(); 
} 

[HttpPost] 
public IActionResult Create(ProductViewModel model) 
{ 
    // Save the image to desired location and retrieve the path 
    // string ImagePath = ...   

    // Add to db 
    _repository.Add(new Product 
    { 
     Id = model.Id, 
     ImagePath = ImagePath, 
     // and so on 
    }); 

    return View(); 
} 

また、あなたのビューでフォームenctype="multipart/form-data"に指定します。

+0

しかし、私は画像をサーバーのフォルダに保存したいだけで、ImagePathをDBに保存したいだけです。それは私がバイト[]の代わりにIFormFileを使用した理由です。 –

+0

私は自分の答えを改訂しました。 –

+0

さて、最初の投稿を見て、HttpPostメソッドを更新して追加しました。問題はImageがNULLになることです。 ImageがNULLをとる理由は何ですか? –

関連する問題