如何解决“System.ArgumentException:Value不能为null或为空。参数名称:contentPath”这个错误?

时间:2016-12-19 12:27:20

标签: asp.net-mvc

以下是我的updateproduct.cshtml文件,我收到错误。

@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此错误。

所以请帮助我解决这个错误。

1 个答案:

答案 0 :(得分:0)

您视图中的这部分代码可能会引发contentPath异常:

<img src="@Url.Content(Model.ProductImage)" alt="IMAGES" height="300" ; width="300" />

如果Model.ProductImage值为null或为空而不是在传递给视图时包含相对路径字符串,则它将抛出contentPath异常,因为Url.Content方法不能具有null或空参数。< / p>

要解决内容URL问题,请对ProductImage中的UpdateTheProduct属性(GET或POST操作方法或两者)中的null或空值进行if-condition检查,并设置其默认值使用相对路径:

if (String.IsNullOrEmpty(Model.ProductImage))
{
    Model.ProductImage = "~/path/imagefile";
}

// some code
return View(product);

相关问题:

Value cannot be null or empty. Parameter name: contentPath

"Value cannot be null or empty. Parameter name: contentPath" on a most unexpected line on postback when ModelState has errors