[英]ASP.NET Core 5 MVC: ArgumentNullException: Value cannot be null. (Parameter 'items')
[英]ASP.NET MVC ArgumentNullException: Value cannot be null. (Parameter 'source')
我有一个 asp.net mvc 应用程序。 我想执行和更新操作。我想在解释我的问题之前向您展示我的代码抱歉,我的英语不流利,这就是为什么我更喜欢先展示我的代码。这里我有一个索引页面,显示数据库中的所有项目桌子
@model IEnumerable<InOut.Models.Item>
@if (Model.Count() > 0)
{
<table class="table table-bordered table-striped" style="width: 100%; text-align: center">
<thead>
<tr>
<th>Item Name</th>
<th>Borrower</th>
<th>Lender</th>
<th >Edit</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td style="width: 25%">@item.borrower</td>
<td style="width: 25%">@item.Lender</td>
<td style="width: 25%">@item.ItemName</td>
<td style="width: 13%">
<a asp-controller="Item" asp-action="Delete" asp-route-id="@item.id" onclick="showAlert()"class="btn btn-danger" id="btn1" style="text-decoration: none; color: white">delete</a>
<a asp-controller="Item" asp-action="Update" asp-route-id="@item.id" class="btn btn-primary" id="btn1" style="text-decoration: none; color: white">Update</a>
</td>
</tr>
}
</tbody>
</table>
}
else
{
<p> No Items created </p>
}
这是该视图的索引操作
public IActionResult Index()
{
IEnumerable<Item> items = _dbContext.Items;
return View(items);
}
在 foreach 循环中,我有一个按钮来更新该行上的 object 它在 controller class 中有一个名为“更新”的操作。这是获取该行上的项目的代码。
//Get Item
public IActionResult Update(int? id)
{
var item = _dbContext.Items.Single(o => o.id == id);
return View(item);
}
此操作将所选项目属性发送到更新视图。这是视图代码
<form method="post" asp-action="Create">
<input asp-for="id" hidden/>
<div class="form-group">
<label >Item Name </label>
<input type="text" class="form-control" aria-describedby="emailHelp" asp-for="ItemName">
</div>
<div class="form-group">
<label >Borrower</label>
<input type="text" class="form-control" asp-for="borrower">
</div>
<div class="form-group">
<label >Lender</label>
<input type="text" class="form-control" asp-for="Lender">
</div>
<button asp-controller="Item" asp-action="Update" type="submit" class="btn btn-primary" style="margin-top: 10px">Submit</button>
提交按钮将属性发送到带有项目参数的重载操作更新。 更新操作后,我希望它显示索引页。 这是重载的更新操作
// Update Item
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Update(Item item)
{
_dbContext.Update(item);
_dbContext.SaveChanges();
return View("Index");
}
问题从这里开始。 当我在提交更改后运行应用程序时,我希望它向我展示索引操作更新操作运行良好。但是当它试图向我展示索引页面时,它给我这个错误。
ArgumentNullException: Value cannot be null. (Parameter 'source') 它指的是索引页中的 if 语句
现在,如果我使用 redirectToAction 返回而不是像这样查看...
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Update(Item item)
{
_dbContext.Update(item);
_dbContext.SaveChanges();
return RedirectToAction("Index");
}
它按我的意愿工作。 我想知道第一种方法有什么问题,这两种返回类型之间有什么区别吗?
区别就在这里
public IActionResult Index()
{
IEnumerable<Item> items = _dbContext.Items; //maybe you need to add .ToList(); ??
return View(items);
}
当您使用索引操作时,您将项目列表创建为 model 并将它们传递给视图。 但是当您从更新操作调用索引视图时,您不会创建任何 model。 如果您仍然想从更新中返回视图,您可以这样做
_dbContext.Update(item);
_dbContext.SaveChanges();
var items = _dbContext.Items.ToList();
return View("Index", items);
或者如果更新和索引操作在同一个 controller 我通常只调用一个操作作为方法
_dbContext.Update(item);
_dbContext.SaveChanges();
return Index();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.