2017-04-14 4 views
2

私は他の質問やチュートリアルを見てきましたが、私は既存の作品にその提案を組み込むのに苦労しています。ASP.NET MVC 5の画像をアップロードするEF6

新しいリスティングの投稿フォームに画像をアップロードできるようにするため、私のリスティングモデルは次のようになります。

public class Listing 
{ 
    public int ListingID { get; set; } 
    [Display(Name = "Select Category")] 
    public int CategoryID { get; set; } 
    [Display(Name = "Select Vendor")] 
    public int VendorID { get; set; } 
    [Required] 
    [Display(Name = "Listing Name")] 
    public string ListingName { get; set; } 
    [Required] 
    [Display(Name = "Listing Description")] 
    public string ListingDesc { get; set; } 
    [Required] 
    [Display(Name = "Maximum Capacity")] 
    public int Capacity { get; set; } 
    [Required] 
    [DataType(DataType.Currency)] 
    [Display(Name = "Price")] 
    public decimal Price { get; set; } 
    [Required] 
    [Display(Name = "Fixed Price?")] 
    public bool IsFixedPrice { get; set; } 
    [Required] 
    public string Address { get; set; } 
    [Display(Name = "Address Line 2")] 
    public string Address2 { get; set; } 
    [Required] 
    public string City { get; set; } 
    [Required] 
    public string Postcode { get; set; } 

    public virtual Category Category { get; set; } 
    public virtual Vendor Vendor { get; set; } 

} 

私は別のコントローラを持っていますが、作成方法は次のとおりです。

// GET: Listing/Create 
    public ActionResult Create() 
    { 
     ViewBag.CategoryID = new SelectList(db.Categories, "CategoryID", "CategoryName"); 
     ViewBag.VendorID = new SelectList(db.Vendors, "ID", "CompanyName"); 
     return View(); 
    } 

    // POST: Listing/Create 
    // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
    // more details see https://go.microsoft.com/fwlink/?LinkId=317598. 
    [HttpPost] 
    [ValidateAntiForgeryToken] 
    public ActionResult Create([Bind(Include = "ListingID,CategoryID,VendorID,ListingName,ListingDesc,Capacity,Price,IsFixedPrice,Address,Address2,City,Postcode")] Listing listing) 
    { 
     if (ModelState.IsValid) 
     { 
      db.Listings.Add(listing); 
      db.SaveChanges(); 
      return RedirectToAction("Index"); 
     } 

     ViewBag.CategoryID = new SelectList(db.Categories, "CategoryID", "CategoryName", listing.CategoryID); 
     ViewBag.VendorID = new SelectList(db.Vendors, "ID", "CompanyName", listing.VendorID); 
     return View(listing); 
    } 

私は私の既存のコードにこの一見かなり単純なタスクを実装する方法上の任意の方向をいただければ幸いです!

+0

入力ファイルタイプのタグを使用して、モデルの新しいプロパティにバインドしてみましたか?あなたはそれを行うことができ、他のすべてをほぼ同じように扱うことができるはずです。 – DevNoob

答えて

0

Uploadsディレクトリに1つのファイルをアップロードできるソリューションが見つかりました。

私のモデルは変更されません。

コントローラーが更新されました。

HttpPostedFileBase file 

そして、

if (file.ContentLength > 0) 
     { 
      string fileName = Path.GetFileName(file.FileName); 
      string directory = Server.MapPath("~/Content/uploads/"); 
      if (!Directory.Exists(directory)) 
      { 
       Directory.CreateDirectory(directory); 
      } 
      string path = Path.Combine(directory, fileName); 
      file.SaveAs(path); 

     } 

これは私の元のコードのコンテキストでこれを形成します。

// POST: Listing/Create 
    // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
    // more details see https://go.microsoft.com/fwlink/?LinkId=317598. 
    [HttpPost] 
    [ValidateAntiForgeryToken] 
    public ActionResult Create([Bind(Include = "ListingID,CategoryID,VendorID,ListingName,ListingDesc,Capacity,Price,IsFixedPrice,Address,Address2,City,Postcode")] Listing listing, HttpPostedFileBase file) 
    { 

     if (file.ContentLength > 0) 
     { 
      string fileName = Path.GetFileName(file.FileName); 
      string directory = Server.MapPath("~/Content/uploads/"); 
      if (!Directory.Exists(directory)) 
      { 
       Directory.CreateDirectory(directory); 
      } 
      string path = Path.Combine(directory, fileName); 
      file.SaveAs(path); 

      //var fileName = Path.GetFileName(file.FileName); 
      //var path = Path.Combine(Server.MapPath("~/Content/Uploads")); 
      //file.SaveAs(path); 
     } 

     if (ModelState.IsValid) 
     { 

      db.Listings.Add(listing); 
      db.SaveChanges(); 
      return RedirectToAction("Index"); 
     } 

     ViewBag.CategoryID = new SelectList(db.Categories, "CategoryID", "CategoryName", listing.CategoryID); 
     ViewBag.VendorID = new SelectList(db.Vendors, "ID", "CompanyName", listing.VendorID); 
     return View(listing); 
    } 

私の作成ビューでは、

@using (Html.BeginForm("Create", "Listing", FormMethod.Post, new { enctype = "multipart/form-data" })) 

そして、

<input type="file" name="file" id="file" /> 
<input type="submit" value="submit" /> 

これは、私の作品は、しかし、私は、個々のリストのIDを表示しようとすると、それがどのように実行するか確認するためにテストされていませんしましたので、これは私のための正しい解決策ではないかもしれません。

関連する問題