2017-04-14 132 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

我找到了一個解決方案,它可以讓我上傳單個文件到上傳目錄。

我的模型保持不變。

控制器已更新爲包括;

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時,它如何執行,所以這可能不是對我來說是正確的解決方案。