以下是我的updateproduct.cshtml
文件,其中出現錯誤。如何解決「System.ArgumentException:值不能爲空或空。參數名稱:contentPath」這個錯誤?
@model ShopperDB.Context.Product
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h2 class="admin-title text-center">Edit Product</h2>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.ProductID)
<div class="form-group">
@Html.LabelFor(model => model.ProductName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-6">
@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.ProductImage, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-6">
<img src="@Url.Content(Model.ProductImage)" alt="IMAGES" height="300" ; width="300" />
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-6">
@Html.EditorFor(model => model.Description, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Description, "", 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-6">
@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.CategoryID, "CategoryName", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-6">
@Html.DropDownList("CategoryID", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.CategoryID, "", new { @class = "text-danger" })
</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>
}
</div>
</div>
</div>
</div>
}
以下是從產品控制器我更新產品的方法。
//Update the product
[HttpGet]
[Route("product/update/{id}")]
[CustomAuthorize("admin")]
public ActionResult UpdateTheProduct(int? id)
{
if (Session["UserName"].ToString() != null)
{
if (Session["UserName"].ToString() == "admin")
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Product product = db.Products.Find(id);
if (product == null)
{
return HttpNotFound();
}
ViewBag.CategoryID = new SelectList(db.ProductCategories, "CategoryID", "CategoryName");
return View(product);
}
else
{
return RedirectToAction("Index", "Home");
}
}
else
{
return RedirectToAction("Index", "Home");
}
}
[HttpPost]
[ValidateAntiForgeryToken]
[Route("product/update/{id}")]
[CustomAuthorize("admin")]
public ActionResult UpdateTheProduct([Bind(Include = "ProductID,ProductName,ProductImage,Description,Price,CategoryID")] Product product)
{
if (ModelState.IsValid)
{
if (db.Products.Any(ac => ac.ProductName.Equals(product.ProductName)))
{
TempData["fail"] = "Category already Added";
}
else
{
TempData["notice"] = "Successfully Added";
db.Entry(product).State = EntityState.Modified;
db.SaveChanges();
}
return RedirectToAction("ListOfProducts");
}
return View(product);
}
而我更新的產品它顯示我System.ArgumentException: Value cannot be null or empty.Parameter name: contentPath
這個錯誤上存儲ProductImage
點。
所以請善意幫我解決這個錯誤。
代碼太多 - 只顯示相關的內容 –