2016-12-19 40 views
-1

以下是我的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點。

所以請善意幫我解決這個錯誤。

+0

代碼太多 - 只顯示相關的內容 –

回答

0

的代碼在你看來這部分潛在投contentPath例外:

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

如果Model.ProductImage值爲null或空的,而不是包含傳遞給觀看時的相對路徑字符串,它會拋出contentPath異常,因爲Url.Content方法不能有空或空參數。

爲了解決內容URL問題,將如果條件對證空或空值上ProductImage財產UpdateTheProduct(GET或POST操作方法,或兩者​​)並使用相對路徑設置爲默認值:

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

相關問題