我已经看了一些其他问题和教程,但我很难将这些建议纳入我现有的工作中。
我希望用户能够在提交表单中上传新图片的图片,我的列表模型如下所示;
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; }
}
我有一个单独的控制器,create方法如下;
// 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 :(得分:0)
我找到了一个可以让我将单个文件上传到Uploads目录的解决方案。
我的模型保持不变。
控制器已更新为包含;
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时,我还没有测试过它是如何执行的,所以这对我来说可能不是正确的解决方案。