3

I have problem in my code. I try to Edit detail of product and add an field "Category" and "Publisher" as a DropdownList while update Detail like this image: here

This is my code in controller:

[HttpGet]
public IActionResult Edit_Products(string productId)
{
    ViewBag.ListofPublisher = context.Publisher.ToList();
    ViewBag.ListofCategory = context.Category.ToList();

    return View(context.Product.Find(productId));
}

In the View,I add two dropdownList to loads Category and Publisher and post back to controller

<div class="form-group">
    <label>Category</label>
    <br />
    <select class="form-control" asp-for="CategoryCode"
            asp-items="@(new SelectList(ViewBag.ListofCategory,"CategoryCode","CategoryName"))">
    </select>
</div>
@*Product Category*@
<div class="form-group">
    <label>Publisher</label>
    <br />
    <select class="form-control" asp-for="PublisherCode"
            asp-items="@(new SelectList(ViewBag.ListofPublisher,"PublisherCode","PublisherName"))">
    </select>
    </div>
    @*Product Publisher*@

In Edit controller, I will update this to table

[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Edit_Products(Product product,string productCode)
{
    if (ModelState.IsValid)
    {
        Product result = context.Product.SingleOrDefault(p => p.ProductCode.Equals(product.ProductCode));

        try
        {
            string proCode = product.ProductCode;
            result.ProductCode = (product.CategoryCode) + (product.PublisherCode) + (proCode.Substring(proCode.Length - 2));
            result.ProductName = product.ProductName;
            result.Price = product.Price;
            result.Quantity = product.Quantity;
            result.AuthorName = product.AuthorName;
            result.ReleaseYear = product.ReleaseYear;
            result.Ver = product.Ver;
            result.Used = product.Used;
            result.Review = product.Review;
            result.CategoryCode = product.CategoryCode;
            result.PublisherCode = product.PublisherCode;
            result.Picture = product.Picture;

            context.SaveChanges();
        }
        catch (Exception exx)
        {
           ViewBag.Msg = exx.Message;
        }

        return RedirectToAction("Index", "Manage_Products");
    }

    return View();
}

Then I got this error:

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

The error code is reported in two lines Select in View

Please help me.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Eric
  • 43
  • 1
  • 5
  • Which line in the code throws the error? – Chetan Dec 13 '20 at 09:05
  • Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – JohnG Dec 13 '20 at 09:06
  • The error code is reported in two lines Select in View – Eric Dec 13 '20 at 09:18
  • From what I can tell… `asp-items="@(new SelectList(ViewBag.ListofCategory,"CategoryCode","CategoryName"))"> ` … or the list of publishers line is returning a `null` value. – JohnG Dec 13 '20 at 09:23
  • How can I set up the Value for select? – Eric Dec 13 '20 at 09:31
  • I will assume the “set up” is correct. The compiler isn’t complaining about the “set up”… it is complaining that… “when set up” is complete, the code is passing it a `null` value in one of the parameters. I would assume it may be one of the lists… `ViewBag.ListofCategory` or `ViewBag.ListofPublisher`. Since these appear to possibly be “new” lists… are you use sure the lists are getting “initialized” in the containing class? – JohnG Dec 13 '20 at 09:39
  • I'm sure I instantiated it in containing class – Eric Dec 13 '20 at 09:59
  • 1
    Well… the error seems pretty clear… one or both of the lists is `null`. If the lists are getting initialized, then something else must be making one or both of the lists `null`. Have you traced this, like putting a breakpoint on the code and checking the lists to see if they may be `null`? This would be trouble shooting 101. – JohnG Dec 13 '20 at 10:10
  • Does this happen when your post ends up with `ModelState.IsValid == false`? If yes - you need to "refill" the `ViewBag` with values cause they are not shared between the requests. – Guru Stron Dec 13 '20 at 10:12

1 Answers1

1

From what I see in the question I would assume that error happens on POST request when your model fails validation (i.e. ModelState.IsValid is false). If that is the case - you need to fill ListofPublisher and ListofCategory again cause the data you put in the ViewBag/ViewData is only available during the life-cycle of the request within which you populated it (read more here). i.e. call:

ViewBag.ListofPublisher = context.Publisher.ToList();
ViewBag.ListofCategory = context.Category.ToList();

Before return View(); in your POST method.

Guru Stron
  • 102,774
  • 10
  • 95
  • 132