ASP.NET MVC ArgumentNullException:值不能为空。 (参数'source')

ASP.NET MVC ArgumentNullException: Value cannot be null. (Parameter 'source')

我有一个 asp.net mvc 应用程序。我想执行和更新 operation.I 想在解释我的问题之前向您展示我的代码 抱歉,我的英语不流利,这就是为什么我更愿意展示我的代码 first.Here 我有一个显示所有项目的索引页面在数据库中 table

 @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 循环中,我有一个按钮来更新该行上的对象它在控制器中有一个名为“更新”的操作 class.here 是获取该行上的项目的代码。

//Get Item
public IActionResult Update(int? id)
{
    var item = _dbContext.Items.Single(o => o.id == id);
    return View(item);
}

此操作将所选项目属性发送到更新view.here是视图的代码

<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");
}

问题从这里开始。当我在提交更改后 运行 应用程序时,我希望它向我显示索引操作更新操作正在工作 well.but 当它试图向我显示索引页面时它给我这个错误。

ArgumentNullException:值不能为空。 (参数'source') it

引用索引页中的if语句

现在如果我使用 redirectToAction return 而不是这样的视图...

 [HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Update(Item item)
{
    _dbContext.Update(item);
    _dbContext.SaveChanges();
    return RedirectToAction("Index");
}

如我所愿。我想知道第一种方法有什么问题,这两种 return 类型之间有什么区别吗?

区别就在这里

  public IActionResult Index()
{
 IEnumerable<Item> items = _dbContext.Items; //maybe you need to add .ToList(); ??
    return View(items);
}

当您使用索引操作时,您会创建项目列表作为模型并将它们传递给视图。但是当您从更新操作调用索引视图时,您不会创建任何模型。如果您仍想 return 从更新中查看,您可以这样做

_dbContext.Update(item);
    _dbContext.SaveChanges();
 var  items = _dbContext.Items.ToList();
    return View("Index", items);

或者如果更新和索引操作在同一个控制器中,我通常只是将操作作为方法调用

_dbContext.Update(item);
    _dbContext.SaveChanges();

    return Index();