У меня есть LaptopController с ActionResult Edit.Я создал режим редактирования бритвы в asp.net mvc.Но когда я редактирую, он возвращает исключение
Вот код контроллера
[HttpGet]
public ActionResult Edit(int id)
{
LAPTOP laptop = data.LAPTOPs.SingleOrDefault(n => n.ID == id);
if (laptop == null)
{
Response.StatusCode = 404;
return null;
}
ViewBag.IDM = new SelectList(data.manufacturers.ToList().OrderBy(n => n.ManufacturerName), "IDM", "ManufacturerName", laptop.IDM);
return View(laptop);
}
[HttpPost]
[ValidateInput(false)]
public ActionResult SuaSP(LAPTOP laptop, HttpPostedFileBase fileUpload)
{
ViewBag.IDM = new SelectList(data.manufacturers.ToList().OrderBy(n => n.ManufacturerName), "IDM", "ManufacturerName");
var fileName = Path.GetFileName(fileUpload.FileName);
var path = Path.Combine(Server.MapPath("~/Images"), fileName);
fileUpload.SaveAs(path);
laptop.ImageCover = fileName;
//UpdateModel(laptop);
data.SubmitChanges();
return RedirectToAction("Laptop");
}
Строка, вызывающая исключение:
var fileName = Path.GetFileName(fileUpload.FileName);
при отладкескажите мне, что fileName было пустым
это код для редактирования:
@model GoodLaptop.Models.LAPTOP
@ с использованием (Html.BeginForm (new {enctype = "multipart / form-data"})) {
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Laptop</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.ID)
<div class="form-group">
@Html.LabelFor(model => model.ProductName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ProductName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ProductName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Price, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Price, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Price, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ImageCover, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
Chọn ảnh mới
<input type="file" name="fileUpload" />
<img src="@Url.Content("~/Images/"+ Model.ImageCover)" width="120" />(Ảnh hiện tại)
@ViewBag.Thongbao
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.UpdateDate, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.UpdateDate, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.UpdateDate, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Amount, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Amount, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Amount, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.IDM, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("IDM")
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
Пожалуйста, помогите, большое спасибо