这是否有效;在一个阶段。问题是以下文本现在出现在数据库“System.Web.HttpPostedFileWrapper”中的图像的字段中
以下代码来自控制器:
[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");
}
此代码来自创建视图:
@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>
}
答案 0 :(得分:2)
HttpPostedFileBase控制器中的参数必须与input type =“file”具有相同的名称。要么:
[HttpPost]
public ActionResult Create(CarAdvert caradvert,
HttpPostedFileBase Image1)
或
<input type="file" name="picture1" />
应该这样做