我正在處理列表網站,並試圖上傳圖像並將其存儲在數據庫中。ASP.NET MVC 4無法從表單中檢索文件
問題是,我似乎無法從窗體中檢索上傳的文件。
這裏是形式,
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>Product</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Description)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Description)
@Html.ValidationMessageFor(model => model.Description)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Location)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Location)
@Html.ValidationMessageFor(model => model.Location)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Condition)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Condition)
@Html.ValidationMessageFor(model => model.Condition)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Price)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Price)
@Html.ValidationMessageFor(model => model.Price)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.PostedBy)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.PostedBy)
@Html.ValidationMessageFor(model => model.PostedBy)
</div>
<div class="editor-field">
Upload Image <input type="file" name="listingImage" />
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
這裏是行動
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Product product, HttpPostedFileBase listingImage)
{
if (listingImage == null)
{
return RedirectToAction("Index");
}
if (ModelState.IsValid)
{
db.Products.Add(product);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(product);
}
我有一個模型,一個產品上市的設置,像這樣。
public class Product
{
public int ID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Location { get; set; }
public string Condition { get; set; }
public decimal Price { get; set; }
public string PostedBy { get; set; }
public string PhotoPath { get; set; }
}
現在,每次填寫表格並提交listingImage爲空,我都會返回索引。
有什麼我失蹤了嗎?
謝謝。
非常感謝,這是我尋找的解決方案。 – jaooe