Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
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 :
C#
[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
C#
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

C#
     @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 :

C#
@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"
Posted
Updated 9-May-14 9:23am
v6
Comments
Sunasara Imdadhusen 9-May-14 0:18am    
What is going wrong using this code? can you please let us know does it gives you an error or what!
mgsdew 9-May-14 0:59am    
@Sunasara Imdadhusen<br>
When I click 'Product' entry at main page then browser doesn't show any response and following error line shows in project "Value cannot be null or empty.\r\nParameter name: contentPath"

1 solution

Add a line after in Index view of Product controller
@Html.RenderPartial("myview",Model)
 
Share this answer
 
Comments
mgsdew 9-May-14 11:45am    
@sankarsan parida

Hey,in your given solution "myview" is not the name of partial view ??
I write it but compiler doesn't resolve the position of it..
[no name] 9-May-14 22:55pm    
ooh no I just given one example. YOu have to give your partial view name, right.
mgsdew 10-May-14 0:28am    
I write this code just after finishing tag of table in my Product Controller 'Index'

</table>

@Html.Partial("_ProductList", Model)

[_ProductList is my partial view which is in Shared folder]

and after run the program when I click Product index got this error :-

{"Value cannot be null or empty.\r\nParameter name: contentPath"}
[no name] 10-May-14 1:35am    
see here return View(uploadedFiles); uploaddedFiles is null or not. check a condition before partial view or inside the partial view model is null or not
like: @if(Model !=null)@Html.RenderPartial("_ProductList",Model)
mgsdew 10-May-14 7:16am    
In your given way partial view show in Index view. But when I enter in Create view for uploading image and press Create button then it show same error message.
Actually my main problem is uploading image and show it Index view..that's why I want to create a Partial view and merge it with my target Index view.. Have you any better solution for it ??

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900