0

I keep getting an error when trying to delete a record from my database using MVC. What does this error mean? What am I doing wrong?

Here is my controller action:

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

Here is my View:

@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>

Edit and Create Work Fine. When I added the delete is where i started to get the issues. Here is the exception I am getting and Model is null FYI

An exception of type 'System.NullReferenceException' occurred in App_Web_rczw3znb.dll but was not handled in user code
John Saunders
  • 160,644
  • 26
  • 247
  • 397
user977154
  • 1,045
  • 4
  • 19
  • 39
  • Yes, you get null exceptions because your view tries to access the model, but you don't pass any model to it... so the model is null. Not very difficult to understand why. – Erik Funkenbusch May 09 '14 at 02:54
  • This won't fix your problem, but just FYI, `x.Where(condition).FirstOrDefault()` can be written as `x.FirstOrDefault(condition)`. – Simon MᶜKenzie May 09 '14 at 06:16

2 Answers2

1

You need to supply a model for the Index view. You're doing this in Create and Edit.. but you aren't in Delete.

So, something like this:

return View("Index", listOfPeopleHere);

Also, you seem to be using a View as an entire page. You should really look into using a Layout.. so that the boilerplate code at the top of the page isn't required in every view.

Simon Whitehead
  • 63,300
  • 9
  • 114
  • 138
0
    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);
    }
user977154
  • 1,045
  • 4
  • 19
  • 39