ASP.NET MVC > Entity Framework

Delete record from database in ASP.NET MVC

How to delete a record from the database?


To delete a record from database, again we will be using the same scaffolding template.

CONTROLLER ACTION METHODS

// GET: PersonalDetails/Delete/5
public ActionResult Delete(int? id)
{
    if (id == null)
    {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }
    PersonalDetail personalDetail = db.PersonalDetails.Find(id);
    if (personalDetail == null)
    {
        return HttpNotFound();
    }
    return View(personalDetail);
}

// POST: PersonalDetails/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
    PersonalDetail personalDetail = db.PersonalDetails.Find(id);
    db.PersonalDetails.Remove(personalDetail);
    db.SaveChanges();
    return RedirectToAction("Index");
}

In the first Delete action method, we find out the value of id; if null redirects to Bad Request otherwise finds the record and return to Delete view with PersonalDetail model.

DELETE VIEW CODE

@model WebApplication1.Models.PersonalDetail

@{
 ViewBag.Title = "Delete";
}

<h2>Delete</h2>

<h3>Are you sure you want to delete this?</h3>
    <div>
        <h4>PersonalDetail</h4>
        <hr />
        <dl class="dl-horizontal">
            <dt>@Html.DisplayNameFor(model => model.FirstName)
            </dt>
            <dd>@Html.DisplayFor(model => model.FirstName)
            </dd>
            <dt>@Html.DisplayNameFor(model => model.LastName)
            </dt>
            <dd>@Html.DisplayFor(model => model.LastName)
            </dd>
            <dt>@Html.DisplayNameFor(model => model.Age)
            </dt>
            <dd>@Html.DisplayFor(model => model.Age)
            </dd>
            <dt>@Html.DisplayNameFor(model => model.Active)
            </dt>
            <dd>@Html.DisplayFor(model => model.Active)
            </dd>
        </dl>

        @using (Html.BeginForm()) {
@Html.AntiForgeryToken()
        <div class="form-actions no-color">
            <input type="submit" value="Delete" class="btn btn-default" />
            |
@Html.ActionLink("Back to List", "Index")
        </div>
        }
    </div>

This view retrieve the Model data and show the record selected for delete. Read the How to list …. post in this series to understand about @Html.DisplayNameFor and @Html.DisplayFor methods.

Clicking on Delete button calls DeleteConfirmed action method. Notice the attribute ActionName that instruct the ASP.NET MVC framework that despite the name of the method is DeleteConfirmed, it’s name should be treated as Delete. This is because we can’t keep the same name of the action method with same parameter in C#. This is a trick to fool C# compiler.

DeleteConfirmed method finds the record to delete and add into Remove method and calls SaveChanges() method to physically remove this record from the database.

 Views: 106780 | Post Order: 103



Write for us






Hosting Recommendations