我在选择文件的地方有以下代码。但无论我选择什么文件,我都会得到以下异常:
无法将值 NULL 插入 ImgPath 列。
似乎文件没有将任何值传递回控制器,我做错了吗?
创建.cshtml:
<div class="editor-label">
@Html.LabelFor(model => model.ImgPath)
</div>
<div class="editor-field">
<input type="file" name="file" />
</div>
家庭控制器:
public ActionResult Create(TopSong topsong, HttpPostedFileBase file)
{
if (ModelState.IsValid)
{
//verify user has selected file
if (file != null && file.ContentLength > 0)
{
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath(
"~/Content/themes/base/images/Movie Images"), fileName);
file.SaveAs(path);
}
db.TopSongs.Add(topsong);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.DateId = new SelectList(db.TopDates, "DateId", "DateId",
topsong.DateId);
return View(topsong);
}