我有一個 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 回圈中,我有一個按鈕來更新該行上的物件,它在控制器類中有一個名為“更新”的操作。這里是獲取該行上的專案的代碼。
//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>
提交按鈕將屬性發送到帶有 item 引數的多載操作更新。更新操作后,我希望它顯示索引頁面。這是多載的更新操作
// Update Item
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Update(Item item)
{
_dbContext.Update(item);
_dbContext.SaveChanges();
return View("Index");
}
Problem starts here. When I run application after submitting changes I want it to show me the index action update operation working well.but when it try to show me the index page it gives me this error.
ArgumentNullException: Value cannot be null. (Parameter 'source') and it refers to the if statement in the index page it
now if I use redirectToAction return instead of View like this...
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Update(Item item)
{
_dbContext.Update(item);
_dbContext.SaveChanges();
return RedirectToAction("Index");
}
It works as I wanted. I want to know what is the problem with the first approach and is there any difference between those 2 return types?
uj5u.com熱心網友回復:
區別就在這里
public IActionResult Index()
{
IEnumerable<Item> items = _dbContext.Items; //maybe you need to add .ToList(); ??
return View(items);
}
當您使用索引操作時,您將專案串列創建為模型并將它們傳遞給視圖。但是,當您從更新操作呼叫索引視圖時,您不會創建任何模型。如果您仍想從更新中回傳視圖,您可以這樣做
_dbContext.Update(item);
_dbContext.SaveChanges();
var items = _dbContext.Items.ToList();
return View("Index", items);
或者如果更新和索引操作在同一個控制器中,我通常只是將操作稱為方法
_dbContext.Update(item);
_dbContext.SaveChanges();
return Index();
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/424195.html
標籤:c# .net asp.net-mvc
