I want create a partial view and merge it with main view. But, something is going wrong with my code. Can anyone explain it for me by seeing my given code:-
In my
Product Controller I write those code for upload File/Image :
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Product product)
{
foreach (string file in Request.Files)
{
var postedFile = Request.Files[file];
postedFile.SaveAs(Server.MapPath("~/UploadedFiles/") + Path.GetFileName(postedFile.FileName));
}
if (ModelState.IsValid)
{
db.Products.Add(product);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.CategoryId = new SelectList(db.Categorys, "CategoryId", "Name", product.CategoryId);
ViewBag.SubCategoryId = new SelectList(db.SubCategories, "SubCategoryId", "SubCategoryName", product.SubCategoryId);
ViewBag.ModelId = new SelectList(db.Models, "ModelId", "ModelName", product.ModelId);
return View(product);
}
Then showing File/Image I just write those code in my
Product Controller
public ActionResult List()
{
var uploadedFiles = new List<Product>();
var files = Directory.GetFiles(Server.MapPath("~/UploadedFiles"));
foreach (var file in files)
{
var fileInfo = new FileInfo(file);
var product = new Product() { Name = Path.GetFileName(file) };
product.ImageSize = fileInfo.Length;
product.Path = ("~/UploadedFiles/") + Path.GetFileName(file);
uploadedFiles.Add(product);
}
return View(uploadedFiles);
}
public ViewResult _ProductList()
{
return View("_ProductList");
}
My
Index view of
Product controller is likes
@model IEnumerable<Online_Shopping_Management.Models.Product>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.Category.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.SubCategory.SubCategoryName)
</th>
<th>
@Html.DisplayNameFor(model => model.Model.ModelName)
</th>
<th>
@Html.DisplayNameFor(model => model.ProductName)
</th>
<th>
@Html.DisplayNameFor(model => model.Image)
</th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Category.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.SubCategory.SubCategoryName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Model.ModelName)
</td>
<td>
@Html.DisplayFor(modelItem => item.ProductName)
</td>
<td>
@Html.Partial("~/Views/Shared/_ProductList.cshtml")
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.ProductId }) |
@Html.ActionLink("Details", "Details", new { id=item.ProductId }) |
@Html.ActionLink("Delete", "Delete", new { id=item.ProductId })
</td>
</tr>
}
</table>
My Partial View Model is likes :
@model IEnumerable<Online_Shopping_Management.Models.Product>
@{
ViewBag.Title = "List";
}
<h2>List</h2>
<table>
<tr>
<td> Product Name </td>
<td> Image Name </td>
<td> Preview </td>
</tr>
@foreach (var file in Model)
{
<tr>
<td> @file.ProductName</td>
<td> @file.Name </td>
<td>
<img src="@Url.Content(file.Path)" />
</td>
</tr>
}
</table>
Now after
Create Product when I want to show 'Index' view then browser doesn't show any response and following error line shows in project
"Value cannot be null or empty.\r\nParameter name: contentPath"