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

Praddyumna Sangvikar

以下是我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向我显示此错误

因此,请帮助我解决此错误。

山本哲也

您认为这部分代码可能会引发contentPath异常:

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

如果Model.ProductImagevalue为null或null而不是传递给视图时包含相对路径字符串,则它将抛出contentPath异常,因为Url.Contentmethod不能具有null或empty参数。

要解决内容URL问题,请将if条件检查ProductImage属性中的null或空值UpdateTheProduct(使用GET或POST操作方法,或同时使用这两种方法),然后使用相对路径设置其默认值:

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

// some code
return View(product);

相关问题:

值不能为null或为空。参数名称:contentPath

当ModelState出现错误时,在回发中最意外的行上,“值不能为null或为空。参数名称:contentPath”

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

Related 相关文章

热门标签

归档