【问题标题】:Model Empty Null Exception Error on Delete [duplicate]删除时模型空空异常错误[重复]
【发布时间】:2014-05-09 02:43:23
【问题描述】:

尝试使用 MVC 从我的数据库中删除记录时,我不断收到错误消息。这个错误是什么意思?我做错了什么?

这是我的控制器操作:

public ActionResult Delete(int id)
{

    Person somePerson = db.People
        .Where(p => p.Id == id)     //this line says to find the person whose ID matches our parameter
        .FirstOrDefault();          //FirstOrDefault() returns either a singluar Person object or NULL

    db.Entry(somePerson).State = System.Data.Entity.EntityState.Deleted;
    db.SaveChanges();


    return View("Index");
}

这是我的观点:

@using sample.Models
@model List<Person>
@{

    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <div> 
        <!-- Html.ActionLink is the MVC equivalent of <a href="..."></a>  
            -->
        @Html.ActionLink("Create new person", "Create")
        <table>
            <tr>
                <th></th>
                <th></th>
                <th>Email Address</th>
                <th>First Name</th>
                <th>Last Name</th>
            </tr>

            <!-- Loop through our List of People and create a new TR for each item -->
            @foreach(Person person in Model)  //Error Occurs on this line
            {
                <tr>
                    <td></td>
                    <td>@Html.ActionLink("Edit", "Edit", new { id = person.Id })</td>
                    <td>@Html.ActionLink("Delete", "Delete", new { id = person.Id })</td>
                    <!-- Using the @@ tag will inject the object's value into HTML -->
                    <td>@person.Email</td>
                    <td>@person.FirstName</td>
                    <td>@person.LastName</td>
                </tr>
            }
        </table>
    </div>
</body>
</html>

编辑和创建工作正常。当我添加删除时,我开始遇到问题。这是我得到的异常,模型为空仅供参考

An exception of type 'System.NullReferenceException' occurred in App_Web_rczw3znb.dll but was not handled in user code

【问题讨论】:

  • 是的,你得到空异常,因为你的视图试图访问模型,但你没有将任何模型传递给它......所以模型是空的。不难理解为什么。
  • 这不能解决你的问题,但仅供参考,x.Where(condition).FirstOrDefault() 可以写成x.FirstOrDefault(condition)

标签: c# asp.net-mvc nullreferenceexception


【解决方案1】:

您需要为Index 视图提供模型。您在 CreateEdit.. 中执行此操作,但您不在 Delete 中。

所以,是这样的:

return View("Index", listOfPeopleHere);

此外,您似乎将视图用作整个页面。你真的应该考虑使用Layout.. 这样页面顶部的样板代码就不需要在每个视图中。

【讨论】:

  • 好吧,奇怪的是,在我继续处理异常后它确实删除了记录
  • 当然。您认为该错误正在发生。视图在“删除记录”代码之后呈现。
  • 所以我想要一个完整的人名单而不是一个人?
  • 是的..因为您向我们展示的视图有@model List&lt;Person&gt;
  • 好,我试试
【解决方案2】:
    public ActionResult Delete(int id)
    {
        //Find the person in the DB.  Use the supplied "id" integer as a reference.
        Person somePerson = db.People
            .Where(p => p.Id == id)     //this line says to find the person whose ID matches our parameter
            .FirstOrDefault();          //FirstOrDefault() returns either a singluar Person object or NULL

        if (ModelState.IsValid)
        {
            db.People.Remove(somePerson);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(somePerson);
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多