Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
1.00/5 (5 votes)
Error  
An unhandled exception occurred while processing the request.
ArgumentNullException: Value cannot be null. (Parameter 'items')
Microsoft.AspNetCore.Mvc.Rendering.MultiSelectList..ctor(IEnumerable items, string dataValueField, string dataTextField, IEnumerable selectedValues, string dataGroupField)

Stack Query Cookies Headers Routing
ArgumentNullException: Value cannot be null. (Parameter 'items')
Microsoft.AspNetCore.Mvc.Rendering.MultiSelectList..ctor(IEnumerable items, string dataValueField, string dataTextField, IEnumerable selectedValues, string dataGroupField)
Microsoft.AspNetCore.Mvc.Rendering.SelectList..ctor(IEnumerable items, string dataValueField, string dataTextField, object selectedValue)
Microsoft.AspNetCore.Mvc.Rendering.SelectList..ctor(IEnumerable items, string dataValueField, string dataTextField)
CallSite.Target(Closure , CallSite , Type , object , string , string )
System.Dynamic.UpdateDelegates.UpdateAndExecute4<t0, t1,="" t2,="" t3,="" tret="">(CallSite site, T0 arg0, T1 arg1, T2 arg2, T3 arg3)
AspNetCoreGeneratedDocument.Areas_admin_Views_Item_Edit.<executeasync>b__26_0() in Edit.cshtml
+

Error in this line:
HTML
<div class="form-group">
@ResAdmin.lblCategoryName


What I have tried:

class TbItem:
C#
  1  using Sq.Models;
  2  
  3  namespace Sq.BL
  4  {
  5      public class ClsItems
  6      {
  7          LapShopContext contnxt = new LapShopContext();
  8       
  9          public List<tbitem> GetAll()
 10          {
 11              try
 12              {
 13                  return contnxt.TbItems.ToList();
 14              }
 15              catch (Exception)
 16              {
 17                  return new List<tbitem>();
 18              }
 19          }
 20  
 21          public List<vwitem> GetAllItemData(int? CategoryId)
 22          {
 23              try
 24              {
 25  
 26                  return contnxt.VwItems.Where(srch=> 
 27                  (srch.CategoryId==CategoryId||CategoryId==null||
 28                   CategoryId==0)).ToList();
 29              }
 30              catch (Exception)
 31              {
 32                  return new List<vwitem>();
 33              }
 34          }
 35  
 36          public TbItem GetById(int? id)
 37          {
 38              try
 39              {
 40                 // LapShopContext dbSoqContext = new LapShopContext();
 41                  var item = contnxt.TbItems.FirstOrDefault
 42                             (c => c.ItemId == id);
 43  
 44                  return item;
 45              }
 46              catch
 47              {
 48                  return new TbItem();
 49              }
 50          }
 51  
 52          public bool Save(TbItem item)
 53          {
 54              try
 55              {
 56                  LapShopContext ctx = new LapShopContext();
 57  
 58                  if (item.ItemId == 0)
 59                  {
 60                      item.CreatedBy = "xm";
 61                      item.CreatedDate = DateTime.Now;
 62                      ctx.TbItems.Add(item);
 63                  }
 64                  else
 65                  {
 66                      item.CreatedBy = "xm";
 67                      item.CreatedDate = DateTime.Now;
 68                      ctx.Entry(item).State = 
 69                      Microsoft.EntityFrameworkCore.EntityState.Modified;
 70                  }
 71  
 72                  ctx.SaveChanges();
 73                  return true;
 74              }
 75              catch (Exception)
 76              {
 77                  return false;
 78              }
 79          }
 80  
 81          public bool Delete(int id)
 82          {
 83              try
 84              {
 85                  LapShopContext ctx = new LapShopContext();
 86                  var item = GetById(id);
 87                  ctx.TbItems.Remove(item);
 88  
 89                  ctx.SaveChanges();
 90                  return true;
 91              }
 92              catch (Exception)
 93              {
 94                  return false;
 95              }
 96          }
 97      }
 98  }

My controller:
C#
  1  using Microsoft.AspNetCore.Mvc;
  2  using Sq.BL;
  3  using Sq.Models;
  4  using Sq.Utilities;
  5  
  6  namespace Sq.Areas.admin.Controllers
  7  {
  8      [Area("admin")]
  9      public class ItemController : Controller
 10      {
 11          ClsItems clsItem = new ClsItems();
 12          ClsCategory catList = new ClsCategory();
 13          ClsOs ClsOs = new ClsOs();
 14  
 15          public IActionResult List()
 16          {
 17              ClsItemTypes ClsItemTypes = new ClsItemTypes();
 18              ViewBag.lstItemTypes = ClsItemTypes.GetAll();
 19              ViewBag.lstCategories = catList.GetAll();
 20              var lstItem=clsItem.GetAllItemData(null);   
 21              return View(lstItem);
 22          }
 23         
 24          public ActionResult Edit(int? ItemId)
 25          {
 26              ClsItemTypes ClsItemTypes = new ClsItemTypes();
 27              var item = new Models.TbItem();
 28              ViewBag.lstCategories = catList.GetAll();
 29              ViewBag.lstItemTypes = ClsItemTypes.GetAll();
 30              ViewBag.lstOs = ClsOs.GetAll();
 31  
 32              if (ItemId != null)
 33              {
 34                  item = clsItem.GetById(ItemId);
 35              }
 36  
 37              return View(item);
 38          }
 39  
 40          [HttpPost]
 41          [ValidateAntiForgeryToken]
 42          public async Task<actionresult> 
 43          Save(TbItem item, List<iformfile> Files)
 44          {
 45              if (!ModelState.IsValid)
 46  
 47                  return View("Edit", item);
 48              item.ImageName = await Helper.UploadImage(Files, "items");
 49              clsItem.Save(item);
 50              return RedirectToAction("List");
 51          }
 52  
 53          public IActionResult Search(int id)
 54          {
 55              ViewBag.lstCategories = catList.GetAll();
 56              var items = clsItem.GetAllItemData(id);
 57              return View("List",items);
 58          }
 59      }
 60  }

Error in View Or my Design in this Line:
HTML
<div class="form-group">
    @ResAdmin.lblCategoryName
    
    <span></span>
</div>

If you see my all page
this is my Design:
HTML
  1  @model TbItem
  2  
  3  <div class="content-wrapper">
  4      <div class="page-header">
  5          <h3 class="page-title"> Form elements </h3>
  6          
  7              <ol class="breadcrumb">
  8                  <li class="breadcrumb-item">
  9                  <a href="#">Forms</a></li> 
 10                  <li class="breadcrumb-item active">
 11                  Form elements</li>
 12              </ol>        
 13      </div>    
 14          <div class="row">
 15  
 16              <div class="col-md-6 grid-margin stretch-card">
 17                  <div class="card">
 18                      <div class="card-body">
 19                          <h4 class="card-title">
 20                              Default form</h4>
 21                          <p class="card-description"> 
 22                              Basic form layout </p>                        
 23                          
 24                          <div class="form-group">
 25                              @ResAdmin.lblItemName
 26                              
 27                              <span></span>
 28                          </div>
 29  
 30                          <div class="form-group">
 31                              @ResAdmin.lblCategoryName
 32                              
 33                              <span></span>
 34                          </div>
 35  
 36                          <div class="form-group">
 37                              @ResAdmin.lblItemType
 38                              
 39                              <span></span>
 40                          </div>
 41  
 42                          <div class="form-group">
 43                              @ResAdmin.lblOs
 44                              
 45                              <span></span>
 46                          </div>
 47  
 48                          <div class="form-group">
 49                              @ResAdmin.lblSalesPrice
 50                              
 51                              <span></span>
 52                          </div>
 53  
 54                          <div class="form-group">
 55                              @ResAdmin.lblPurchasePrice
 56                              
 57                              <span></span>
 58                          </div>
 59  
 60                          <div class="form-group">
 61                              @ResAdmin.lblWeight                            
 62                              
 63                          </div>
 64  
 65                          <div class="form-group">
 66                              
 67                                  @ResAdmin.lblImage                            
 68  
 69                          </div>
 70  
 71                          @ResGeneral.lblSave
 72                          @ResGeneral.lblNew
 73  
 74                      </div>
 75                  </div>
 76              </div>
 77  
 78              <div class="col-md-6 grid-margin stretch-card">
 79                  <div class="card">
 80                      <div class="card-body">
 81                          <h4 class="card-title">
 82                          Main Data</h4>
 83                          <p class="card-description"> 
 84                          Basic form layout </p>
 85  
 86                          <div class="form-group">
 87                              @ResAdmin.lblGpu                            
 88                              
 89                          </div>
 90  
 91                          <div class="form-group">
 92                              @ResAdmin.lblProcessor                            
 93                              
 94                          </div>
 95  
 96                          <div class="form-group">
 97                              @ResAdmin.lblHardDisk                            
 98                              
 99                          </div>
100  
101                          <div class="form-group">
102                              @ResAdmin.lblRamSize                            
103                              
104                          </div>
105  
106                          <div class="form-group">
107                              @ResAdmin.lblScreenReslution                            
108                              
109                          </div>
110  
111                          <div class="form-group">
112                              @ResAdmin.lblScreenSize                            
113                              
114                          </div>
115                          <span id="tbImgName"></span>
116                          <div id="tbImgSrc">
117                              
118                          </div>
119                      </div>
120                  </div>
121              </div>
122  
123          </div>
124      
125  </div>
126  @section Scripts
127  {    
128          var Upload = {
129              UploadDefaultImg: function (e) {
130                  const file = e.target.files[0];
131                  const reader = new FileReader();
132                  reader.onloadend = () => {
133                      CourseMediaImgName = file.name;
134                      CourseMediaImgBase64 = reader.result.slice
135                      (reader.result.indexOf('base64,') + 7);
136                      $("#tbImgName").html(file.name)
137                      $("#tbImgSrc").html(`<img class="thumbnail" 
138                      src = "${reader.result}"  
139                      style = "width: 200px;
140                      height: 300px;border-radius: 5px;
141                      border: 1px solid #d9d9d9;padding: 1px;" />`)
142                  };
143                  reader.readAsDataURL(file);
144              }
145          }    
146  }</div>
Posted
Updated 12-Sep-23 9:34am
v3

1 solution

This is the problem:
Error
An unhandled exception occurred while processing the request.
ArgumentNullException: Value cannot be null. (Parameter 'items')

So use the debugger to find out where the exception occurs, and why items is null.
 
Share this answer
 

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