我有一个带ActionResult编辑的LaptopController。 我在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
@using(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>
}
请帮助,非常感谢
答案 0 :(得分:0)
您当前正在错误使用@Html.BeginForm()
辅助函数:
@using (Html.BeginForm(new { enctype = "multipart/form-data" }))
{
// form contents
}
此定义与以下重载匹配:
public static MvcForm BeginForm(
this HtmlHelper htmlHelper,
object routeValues
)
由于您将enctype
定义为routeValues
参数,因此enctype
标签的<form>
HTML属性仍未设置为multipart/form-data
和{{1 }}仍包含空值。您应该改用此重载:
HttpPostedFileBase
您的public static MvcForm BeginForm(
this HtmlHelper htmlHelper,
string actionName,
string controllerName,
FormMethod method,
object htmlAttributes
)
助手将成为此助手,因为您已将控制器操作标记为@Html.BeginForm()
:
[HttpPost]