我正在一个列表站点上工作,我正在尝试上传图像并将其路径存储在数据库中。
问题是,我似乎无法从表单中检索上传的文件。
这是表格,
@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 时,我都会返回到索引。
有什么我想念的吗?
谢谢。