![](/img/trans.png)
[英]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.