[英]ArgumentNullException: Value cannot be null. (Parameter 'items')
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:这是我在 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在视图中,我添加了两个下拉列表来加载类别和发布者并回发到 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在编辑 controller 中,我将其更新为表
[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.
ArgumentNullException:值不能为 null。 (Parameter 'items')
(参数“项目”)
Please help me.请帮我。
From what I see in the question I would assume that error happens on POST request when your model fails validation (ie ModelState.IsValid
is false
).根据我在问题中看到的内容,我假设当您的 model 验证失败(即
ModelState.IsValid
为false
)时,POST 请求会发生错误。 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 ).如果是这种情况 - 您需要再次填写
ListofPublisher
和ListofCategory
,因为您放入 ViewBag/ViewData 的数据仅在您填充它的请求的生命周期内可用( 在此处阅读更多信息)。 ie call:即调用:
ViewBag.ListofPublisher = context.Publisher.ToList();
ViewBag.ListofCategory = context.Category.ToList();
Before return View();
在
return View();
in your POST method.在您的 POST 方法中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.