title: I wanted to perform crud operations for Admin Panel of an ecommerce project but not able to edit image gallery in asp.net core
body: <p>ProductModel  class file</p>
  <pre><code>    public class ProductModel 
      {
              public int ProductID { get; set; }
              
              public string ProductName { get; set; }
              public string ProductDesc { get; set; }
              public string Image { get; set; }
              
              public IFormFile Img { get; set; }
              public IFormFileCollection GalleryImages { get; set; }
              public List&lt;ProductGalleryModel&gt; ProductGallery { get;set;}
      }
  </code></pre>
  <p>ProductGalleryModel class file</p>
  <pre><code>public class ProductGalleryModel
              {
                  public int ID { get; set; }
                  public int ProductId { get; set; }
                  public string  Name { get; set; }
                  public string URL { get; set; }
                  public IFormFileCollection GalleryImages { get; set; }
          
                  public List&lt;ProductGalleryModel&gt; ProductGallery { get; set; }
          
              }
  </code></pre>
  <p>ProductController class file</p>
  <pre><code>public class ProductController : Controller
              {
           private readonly IProductRepository _productRepository = null;
          
          public ProductController(IProductRepository productRepository)
                  {
                      _productRepository = productRepository;
          
                  }
          
           public async Task&lt;IActionResult&gt; AddNewProduct(bool isSuccess = false, int prodId = 0)
                  {
                      ViewBag.Categories = new SelectList(await _categoryRepository.GetAllCategories(), &quot;CatID&quot;, &quot;CategoryName&quot;);
                      ViewBag.IsSuccess = isSuccess;
                      ViewBag.ProductID = prodId;
                      return View();
                  }
          
          [HttpPost]
                  public async Task&lt;IActionResult&gt; AddNewProduct(ProductModel productModel)
                  {
                      if (ModelState.IsValid)
                      {
                          if (productModel.Img1 != null)
                          {
                              string folder = &quot;images/product/cover/&quot;;
                              productModel.Image1 = await UploadImage(folder, productModel.Img1);
                          }
                          if (productModel.GalleryImages != null)
                          {
                              string folder = &quot;images/product/cover/gallery/&quot;;
          
                              productModel.ProductGallery = new List&lt;ProductGalleryModel&gt;();
          
                              foreach (var file in productModel.GalleryImages)
                              {
                                  var gallery = new ProductGalleryModel() 
                                  {
                                          Name = file.FileName,
                                          URL = await UploadImage(folder, file)
                                    };
                                  productModel.ProductGallery.Add(gallery);
                              }
          
                          }
          
                          var id = await _productRepository.AddProduct(productModel);
                          if (id &gt; 0)
                          {
                              return RedirectToAction(nameof(AddNewProduct), new { isSuccess = true, prodId = id });
                          }
                      }
          
                      ViewBag.Categories = new SelectList(await _categoryRepository.GetAllCategories(), &quot;CatID&quot;, &quot;CategoryName&quot;);
                      return View();
                  }
          
          private async Task&lt;string&gt; UploadImage(string folderPath, IFormFile file)
                  {
                      folderPath += Guid.NewGuid().ToString() + &quot;_&quot; + file.FileName;
                      string serverfolderpath = Path.Combine(_webHostEnvironment.WebRootPath, folderPath);
                      await file.CopyToAsync(new FileStream(serverfolderpath, FileMode.Create));
                      return &quot;/&quot; + folderPath;
                  }
          
           public async Task&lt;IActionResult&gt; GetProductGallery(int id)
                  {
                      var data = await _productRepository.GetProductById(id);
                      return View(data);
                  }
          
                  public async Task&lt;IActionResult&gt; EditProductGallery (int id, bool isSuccess = false)
                  {
                      ViewBag.IsSuccess = isSuccess;
                      ViewBag.ProductID = id;
          
                      var ListOfProducts = await _productRepository.GetProductById(id);
                      ViewData[&quot;ProductGallery&quot;] = ListOfProducts.ProductGallery;
                      return View(ListOfProducts);
                  }
           public async Task&lt;PartialViewResult&gt; RenderProduct(int id)
                  {
                      var ListOfProducts = await _productRepository.GetProductById(id);
                     ViewData[&quot;ProductGallery&quot;] = ListOfProducts.ProductGallery;
                      return PartialView(ListOfProducts);
                  }
          
                  public async Task&lt;PartialViewResult&gt; RenderProductGallery(int id)
                  {
                      var ListOfProducts = await _productRepository.getProductGallery(id);
                      ViewData[&quot;ProductId&quot;] = ListOfProducts.ProductGallery;
                      return PartialView(ListOfProducts);
                  }
          
           [HttpPost]
                  public async Task&lt;IActionResult&gt; EditProductGallery(ProductGalleryModel productGalleryModel)
                  {
                      if (ModelState.IsValid)
                      {
                          if (productGalleryModel.URL != null)
                          {
                              if (productGalleryModel.GalleryImages != null)
                              {
                                  string fileName = Path.GetFileName(productGalleryModel.GalleryImages.ToString());
                                  string filePath = Path.Combine(_webHostEnvironment.WebRootPath, fileName);
                                  System.IO.File.Delete(filePath);
          
                                  string folder = &quot;images/product/cover/gallery/&quot;;
                                  productGalleryModel.ProductGallery = new List&lt;ProductGalleryModel&gt;();
          
                                  foreach (var file in productGalleryModel.GalleryImages)
                                  {
                                      var gallery = new ProductGalleryModel()
                                      {
                                          Name = file.FileName,
                                          URL = await UploadImage(folder, file)
                                      };
          
                                     
                                      productGalleryModel.ProductGallery.Add(gallery);
                                  }
                                  await _productRepository.UpdateProductGallery(productGalleryModel.ID, productGalleryModel);
                              }
                          }
                          
                          return RedirectToAction(&quot;GetAllProducts&quot;, new { isSuccess = true, ViewBag.ProductID });
                      }
                      return View();
                  }
          
           
          }
          
          }
  </code></pre>
  <p>ProductRepository for all get and edit methods</p>
  <pre><code>public class ProductRepository : IProductRepository
              {
               public async Task&lt;ProductGalleryModel&gt; getProductGallery(int id)
                  {
                      var result = await _prcContext.tblProductGalleries.Where(x =&gt; x.ProductsProductID == id).
                      Select(getgallerybyid =&gt; new ProductGalleryModel()
                      {
                          URL = getgallerybyid.URL,
                      }).FirstOrDefaultAsync();
                      
                      return result;   
                  }
          
            public async Task&lt;ProductModel&gt; GetProductById(int id)
                  {
                      return await _prcContext.tblProducts.Where(x =&gt; x.ProductID == id).
                          Select(getproductbyId =&gt; new ProductModel()
                          {
                              ProductID = getproductbyId.ProductID,
                              ProductName = getproductbyId.ProductName,
                              ProductDesc = getproductbyId.ProductDesc,
                              Image1 = getproductbyId.Image1,
                              Image2 = getproductbyId.Image2,
                              Image3 = getproductbyId.Image3,
                              Image4 = getproductbyId.Image4,
                              OldPrice = getproductbyId.OldPrice,
                              NewPrice = getproductbyId.NewPrice,
                              Discount = getproductbyId.Discount,
                              CategoryId = getproductbyId.CategoryId,
                              ProductGallery = getproductbyId.productGallery.Select(g =&gt; new ProductGalleryModel()
                              { 
                                      ID = g.GalleryID,
                                      ProductId = g.ProductsProductID,
                                      Name = g.Name,
                                      URL= g.URL
                              }).ToList()
                          }).FirstOrDefaultAsync();
          
                }
          
          public async Task&lt;ProductModel&gt; UpdateProduct(int id, ProductModel productModel)
                  {
                      var updateProduct = await _prcContext.tblProducts.FirstOrDefaultAsync(x =&gt; x.ProductID == id);
                      if (updateProduct != null)
                      {
                          updateProduct.ProductName = productModel.ProductName;
                          updateProduct.ProductDesc = productModel.ProductDesc;
                          updateProduct.Image1 = productModel.Image1;
                          updateProduct.Image2 = productModel.Image2;
                          updateProduct.Image3 = productModel.Image3;
                          updateProduct.Image4 = productModel.Image4;
                          updateProduct.OldPrice = productModel.OldPrice;
                          updateProduct.NewPrice = productModel.NewPrice;
                          updateProduct.Discount = productModel.Discount;
                          updateProduct.CategoryId = productModel.CategoryId;
                      }
                      await _prcContext.SaveChangesAsync();
                      return productModel;
                  }
                  public async Task&lt;ProductGalleryModel&gt; UpdateProductGallery(int id, ProductGalleryModel productGalleryModel)
                  {
          
                      var updateProductGallery = await _prcContext.tblProductGalleries.FirstOrDefaultAsync(x =&gt; x.ProductsProductID == id);
                      if (updateProductGallery != null)
                      {
                          var newproduct = new List&lt;tblProductGallery&gt;();
          
                          foreach (var gallery in newproduct)
                          {
                              updateProductGallery.URL = productGalleryModel.URL;
                          }
                          await _prcContext.SaveChangesAsync();
                      }
                      
                      return productGalleryModel;
                  }
          
          }
  </code></pre>
  <p>Edit Product Gallery View file</p>
  <pre><code> @model ProductGalleryModel
      @{
          ViewData[&quot;Title&quot;] = &quot;Edit Product Gallery&quot;;
          Layout = &quot;~/Areas/Admin/Views/Shared/_AdminLayout.cshtml&quot;;
      }
      
      &lt;style&gt;
          .img-fluid {
              width: 250px;
              height: 250px;
          }
      &lt;/style&gt;
      &lt;div class=&quot;&quot;&gt;
          &lt;!-- Content Header (Page header) --&gt;
          &lt;section class=&quot;content-header&quot;&gt;
              &lt;div class=&quot;container-fluid&quot;&gt;
                  &lt;div class=&quot;row mb-2&quot;&gt;
                      &lt;div class=&quot;col-md-6&quot;&gt;
                          &lt;h1&gt;Edit Product Gallery&lt;/h1&gt;
                      &lt;/div&gt;
                      &lt;div class=&quot;col-md-6&quot;&gt;
                          &lt;ol class=&quot;breadcrumb float-md-right&quot;&gt;
                              &lt;li class=&quot;breadcrumb-item&quot;&gt;&lt;a asp-action=&quot;Index&quot; asp-controller=&quot;Home&quot;&gt;Home&lt;/a&gt;&lt;/li&gt;
                              &lt;li class=&quot;breadcrumb-item&quot;&gt;&lt;a asp-action=&quot;GetAllProducts&quot; asp-controller=&quot;Product&quot;&gt;Products&lt;/a&gt;&lt;/li&gt;
                              &lt;li class=&quot;breadcrumb-item active&quot;&gt;Edit Product Gallery&lt;/li&gt;
                          &lt;/ol&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
              &lt;/div&gt;&lt;!-- /.container-fluid --&gt;
          &lt;/section&gt;
          &lt;section class=&quot;content&quot;&gt;
              &lt;div class=&quot;container-fluid&quot;&gt;
      
                  &lt;div class=&quot;card card-primary py-2&quot;&gt;
                      &lt;div class=&quot;card-header&quot;&gt;
                          &lt;h3 class=&quot;card-title&quot;&gt;Existing Gallery&lt;/h3&gt;
                      &lt;/div&gt;
                      &lt;div class=&quot;card-body&quot;&gt;
      
                          &lt;div class=&quot;row&quot;&gt;
                              &lt;input type=&quot;hidden&quot; value=&quot;@ViewData[&quot;ProductGallery&quot;]&quot; /&gt;
                              &lt;partial name=&quot;~/Areas/Admin/Views/RenderProduct.cshtml&quot;
                                       view-data=&quot;ViewData&quot; /&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
                  &lt;div class=&quot;card card-primary&quot;&gt;
                      &lt;div class=&quot;card-header&quot;&gt;
                          &lt;h3 class=&quot;card-title&quot;&gt;Update Gallery&lt;/h3&gt;
                      &lt;/div&gt;
                      &lt;form method=&quot;post&quot; enctype=&quot;multipart/form-data&quot; asp-action=&quot;EditProductGallery&quot; asp-controller=&quot;Product&quot;&gt;
                          &lt;div class=&quot;card-body&quot;&gt;
      
                              &lt;div class=&quot;row&quot;&gt;
                                  &lt;partial name=&quot;~/Areas/Admin/Views/RenderProductGallery.cshtml&quot; view-data=&quot;ViewData&quot; /&gt;
                                  &lt;div class=&quot;&quot;&gt;
                              &lt;/div&gt;
                          &lt;/div&gt;
      
                          &lt;div class=&quot;card-footer&quot;&gt;
                              &lt;input type=&quot;submit&quot; value=&quot;Update&quot; class=&quot;btn btn-success&quot; /&gt;
                          &lt;/div&gt;
                      &lt;/form&gt;
                  &lt;/div&gt;
      
              &lt;/div&gt;
          &lt;/section&gt;
      &lt;/div&gt;
      
      @section Scripts
      {
          &lt;script&gt;
              
              $(document).ready(function () {
      
                  $('.custom-file-input').on(&quot;change&quot;, function () {
                      var filename = $(this).val().split(&quot;\\&quot;).pop();
                      $(this).next('.custom-file-label').html(filename);
                  });
              });
          &lt;/script&gt;
      }
  </code></pre>
  <p>Product PartialView</p>
  <pre><code>    @model ProductModel
      
          &lt;div class=&quot;col-sm-3&quot;&gt;
              &lt;img src=&quot;@Model.Image1&quot; class=&quot;img-fluid mb-2&quot; alt=&quot;white sample&quot; /&gt;
          &lt;/div&gt;
          @for (int i = 0; i &lt; Model.ProductGallery.Count(); i++)
          {
              &lt;div class=&quot;col-sm-3&quot;&gt;
                  &lt;img src=&quot;@Model.ProductGallery[i].URL&quot; class=&quot;img-fluid mb-2&quot; alt=&quot;white sample&quot; /&gt;
              &lt;/div&gt;
          }
  </code></pre>
  <p>Product Gallery PartialView</p>
  <pre><code>    model ProductGalleryModel
      @{
          ViewData[&quot;ProductGallery&quot;] = true;
      }
      
      &lt;div class=&quot;&quot;&gt;
          &lt;input type=&quot;hidden&quot; value=&quot;@Model.ProductId&quot; /&gt;
          @*&lt;input type=&quot;hidden&quot; value=&quot;@Model.ProductGallery&quot; /&gt;*@
      &lt;/div&gt;
      &lt;div class=&quot;col-md-6&quot;&gt;
          &lt;div class=&quot;form-group&quot;&gt;
              &lt;label asp-for=&quot;GalleryImages&quot;&gt;&lt;/label&gt;
              &lt;div class=&quot;custom-file&quot;&gt;
                  &lt;input asp-for=&quot;GalleryImages&quot; class=&quot;custom-file-input&quot; id=&quot;inputGroupFile01&quot;&gt;
                  &lt;label class=&quot;custom-file-label&quot; for=&quot;inputGroupFile01&quot;&gt;Choose file&lt;/label&gt;
              &lt;/div&gt;
              &lt;span asp-validation-for=&quot;GalleryImages&quot; class=&quot;text-danger&quot;&gt;&lt;/span&gt;
          &lt;/div&gt;
      &lt;/div&gt;
      @section Scripts
      {
          &lt;script&gt;
              $(document).ready(function () {
      
                  $('.custom-file-input').on(&quot;change&quot;, function () {
                      var filename = $(this).val().split(&quot;\\&quot;).pop();
                      $(this).next('.custom-file-label').html(filename);
                  });
              });
          &lt;/script&gt;
      }
  
  </code></pre>
  <p>I am able to return the ProductPartialView with desired results but when i am calling the ProductGalleryPartialView then i am getting an Exception: The model item passed into the ViewDataDictionary is of type 'PRC_webapp.Areas.Admin.Models.ProductModel', but this ViewDataDictionary instance requires a model item of type 'PRC_webapp.Areas.Admin.Models.ProductGalleryModel'.</p>