我收到此错误:值不能为null或为空。参数名称:contentPath。我只是想用" Url.Content"来显示图像。在索引视图中。在调试中进入此页面时,它突破了上述错误:对此有任何帮助将不胜感激。
控制器:
// GET: Customers/Create
public ActionResult Create()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "CustomerID,DiscountLevelID,LoyaltyLevelID,CustomerCompanyName,CustomerName,CustomerSurname,CustomerGUID,CustomerStatus,CustomerAddress,CustomerTel,CustomerCel,CustomerNumber,CustomerContact,CustomerLogo,CustomerLogoPath,LastStoreCustomerSyncID")] Customer customer, HttpPostedFileBase file)
{
if (ModelState.IsValid)
{
if (file != null)
{
string ImageName = System.IO.Path.GetFileName(file.FileName);
string physicalPath = Server.MapPath("~/Images/");
if (!Directory.Exists(physicalPath))
Directory.CreateDirectory(physicalPath);
string physicalFullPath = Path.Combine(physicalPath, ImageName);
file.SaveAs(physicalFullPath);
customer.CustomerLogo = ImageName;
customer.CustomerLogoPath = physicalFullPath;
db.Customers.Add(customer);
db.SaveChanges();
}
return RedirectToAction("Index");
}
return View(customer);
}
索引cshtml
<div class="panel-body">
<a href="@Url.Action("Create","Customers")" class="btn btn-primary">Add Company</a>
<div class="table-responsive">
<table class="table table-striped table-bordered table-hover" id="dataTables-example">
<thead>
<tr>
<th>Company Name</th>
<th>Customer Name</th>
<th>Customer Surname</th>
<th>Address</th>
<th>CustomerLogo</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>@item.CustomerCompanyName</td>
<td>@item.CustomerName</td>
<td>@item.CustomerSurname</td>
<td>@item.CustomerAddress</td>
<td><img src="~/Images/@Url.Content(@item.CustomerLogoPath)" width="100" height="100" /></td>
<td>
<a href="@Url.Action("Edit","Customers", new {id = item.CustomerID} )" class="btn btn-primary"><i class="fa fa-edit "></i> Edit</a> </td>
</tr>
}