这个工作正常吗?在某一阶段。问题是以下文本现在出现在数据库“System.Web.HttpPostedFileWrapper”中的图像字段中
以下代码来自 Controller :
[HttpPost]
public ActionResult Create(CarAdvert caradvert,
HttpPostedFileBase picture1)
{
if (ModelState.IsValid)
{
if (picture1 != null)
{
string image1 = picture1.FileName;
caradvert.Image1 = image1;
var image1Path = Path.Combine(Server.MapPath("~/Content/Images"), image1);
picture1.SaveAs(image1Path);
}
db.CarAdverts.Add(caradvert);
db.SaveChanges();
return RedirectToAction("Index");
}
此代码来自创建 View :
@using (Html.BeginForm("Create", "UserCarAdverts", FormMethod.Post, new { enctype = "multipart/form-data" })) {
@Html.ValidationSummary(true)
<fieldset>
<legend>CarAdvert</legend>
<div class="editor-field">
<input type="file" name="Image1" />
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
最佳答案
HttpPostedFileBase Controller 中的参数必须与输入 type="file"具有相同的名称。要么这样做:
[HttpPost]
public ActionResult Create(CarAdvert caradvert,
HttpPostedFileBase Image1)
或
<input type="file" name="picture1" />
应该可以了
关于asp.net-mvc-3 - 上传图片路径名称为 "System.Web.HttpPostedFileWrapper",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10019931/