Answered by:
ArgumentNullException: Value cannot be null. (Parameter 'items')

Question
-
User-1330485181 posted
Hi,
I have a problem with a DropDownList in ASP.Net Core MVC, currently I am using ASP.NET Core 3.1 When I run my program I got an error "ArgumentNullException: Value cannot be null. (Parameter 'items')" I am creating a Create menu for Products. This menu will contain DropDownList for a field Category. Here I attach the view Create.cshtml for ProductController
@model WebLoginCore.Models.Product @{ ViewData["Title"] = "Create"; } <h1>Create</h1> <h4>Product</h4> <hr /> <div class="row"> <div class="col-md-4"> <form asp-action="Create"> <div asp-validation-summary="ModelOnly" class="text-danger"></div> <div class="form-group"> <label asp-for="asin" class="control-label"></label> <input asp-for="asin" class="form-control" /> <span asp-validation-for="asin" class="text-danger"></span> </div> <div class="form-group"> <label asp-for="asin_image" class="control-label"></label> <input asp-for="asin_image" class="form-control" /> <span asp-validation-for="asin_image" class="text-danger"></span> </div> <div class="form-group"> <label asp-for="asin_descr" class="control-label"></label> <input asp-for="asin_descr" class="form-control" /> <span asp-validation-for="asin_descr" class="text-danger"></span> </div> <div class="form-group"> <label asp-for="textlink" class="control-label"></label> <input asp-for="textlink" class="form-control" /> <span asp-validation-for="textlink" class="text-danger"></span> </div> <div class="form-group"> <label asp-for="CategoryId" class="control-label"></label> <select asp-for="CategoryId" class="form-control" asp-items="@(new SelectList(ViewBag.ListofCategory, "CategoryId", "CategoryName"))" > <option value="">-- Select Category --</option> </select> </div> <div class="form-group"> <input type="submit" value="Create" class="btn btn-primary" /> </div> </form> </div> </div> <div> <a asp-action="Index">Back to List</a> </div> @section Scripts { @{await Html.RenderPartialAsync("_ValidationScriptsPartial");} }
and here is the code for ProductController.cs
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Rendering; using Microsoft.EntityFrameworkCore; using WebLoginCore.Models; namespace WebLoginCore.Controllers { public class ProductsController : Controller { private readonly AmazonContext _context; public ProductsController(AmazonContext context) { _context = context; } // GET: Products public async Task<IActionResult> Index() { List<Category> CategoryList = new List<Category>(); CategoryList = (from d in _context.Categories select d).ToList(); CategoryList.Insert(0, new Category { CategoryId = 0, CategoryName = "Select" }); ViewBag.ListofCategory = CategoryList; return View(await _context.Products.ToListAsync()); } // GET: Products/Details/5 public async Task<IActionResult> Details(int? id) { if (id == null) { return NotFound(); } var product = await _context.Products .FirstOrDefaultAsync(m => m.ProductId == id); if (product == null) { return NotFound(); } return View(product); } // GET: Products/Create public IActionResult Create() { return View(); } // POST: Products/Create // To protect from overposting attacks, enable the specific properties you want to bind to, for // more details, see http://go.microsoft.com/fwlink/?LinkId=317598. [HttpPost] [ValidateAntiForgeryToken] public async Task<IActionResult> Create([Bind("ProductId,asin,asin_image,asin_descr,textlink,CategoryId")] Product product) { if (ModelState.IsValid) { _context.Add(product); await _context.SaveChangesAsync(); return RedirectToAction(nameof(Index)); } return View(product); } // GET: Products/Edit/5 public async Task<IActionResult> Edit(int? id) { if (id == null) { return NotFound(); } var product = await _context.Products.FindAsync(id); if (product == null) { return NotFound(); } return View(product); } // POST: Products/Edit/5 // To protect from overposting attacks, enable the specific properties you want to bind to, for // more details, see http://go.microsoft.com/fwlink/?LinkId=317598. [HttpPost] [ValidateAntiForgeryToken] public async Task<IActionResult> Edit(int id, [Bind("ProductId,asin,asin_image,asin_descr,textlink,CategoryId")] Product product) { if (id != product.ProductId) { return NotFound(); } if (ModelState.IsValid) { try { _context.Update(product); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ProductExists(product.ProductId)) { return NotFound(); } else { throw; } } return RedirectToAction(nameof(Index)); } return View(product); } // GET: Products/Delete/5 public async Task<IActionResult> Delete(int? id) { if (id == null) { return NotFound(); } var product = await _context.Products .FirstOrDefaultAsync(m => m.ProductId == id); if (product == null) { return NotFound(); } return View(product); } // POST: Products/Delete/5 [HttpPost, ActionName("Delete")] [ValidateAntiForgeryToken] public async Task<IActionResult> DeleteConfirmed(int id) { var product = await _context.Products.FindAsync(id); _context.Products.Remove(product); await _context.SaveChangesAsync(); return RedirectToAction(nameof(Index)); } private bool ProductExists(int id) { return _context.Products.Any(e => e.ProductId == id); } } }
and my Category Model is like this below
using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using System.Linq; using System.Threading.Tasks; namespace WebLoginCore.Models { public class Category { public int CategoryId { get; set; } [Required] [MaxLength(50)] [Column(TypeName = "varchar(50)")] public String CategoryName { get; set; } public virtual ICollection<Product> Products { get; set; } } }
Is it correct way to bind a DropDownList ?
Regards,
SentosoFriday, May 22, 2020 7:28 AM
Answers
-
User-1330485181 posted
Problem Solved.
List<Category> CategoryList = new List<Category>(); CategoryList = (from d in _context.Categories select d).ToList(); CategoryList.Insert(0, new Category { CategoryId = 0, CategoryName = "Select" }); ViewBag.ListofCategory = CategoryList;
That code above should be put on procedure public IActionResult Create()
public IActionResult Create() { List<Category> CategoryList = new List<Category>();
CategoryList = (from d in _context.Categories select d).ToList();
CategoryList.Insert(0, new Category { CategoryId = 0, CategoryName = "Select" });
ViewBag.ListofCategory = CategoryList; }- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Saturday, May 23, 2020 12:11 AM
All replies
-
User-1330485181 posted
Problem Solved.
List<Category> CategoryList = new List<Category>(); CategoryList = (from d in _context.Categories select d).ToList(); CategoryList.Insert(0, new Category { CategoryId = 0, CategoryName = "Select" }); ViewBag.ListofCategory = CategoryList;
That code above should be put on procedure public IActionResult Create()
public IActionResult Create() { List<Category> CategoryList = new List<Category>();
CategoryList = (from d in _context.Categories select d).ToList();
CategoryList.Insert(0, new Category { CategoryId = 0, CategoryName = "Select" });
ViewBag.ListofCategory = CategoryList; }- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Saturday, May 23, 2020 12:11 AM -
User303363814 posted
Simultaneous post - glad you worked it out
You seem to have the code which creates ViewBag.ListofCategory in the wrong controller method. The view you have shown seems to be for 'Create' but the 'Create' method does not set the ListofCategory.
Saturday, May 23, 2020 12:11 AM