Wednesday, March 27, 2019

Creating a Delete view using EF Core

In the previous posts we created a List, Details and Edit views using Asp.Net Core MVC and Entity Framework Core to connect to Database view and edit details from SQL Server DB. In this post we will see on how to create a Delete view which will display the details of an Entity object (User) and allow us to delete the User from the database.

Similar to Edit, the delete operation will use 2 action methods. A GET method to get and display user details and a POST method to delete the user from the DB.

        public async Task<IActionResult> Delete(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var users = await _context.Users
                .FirstOrDefaultAsync(m => m.UserId == id);
            if (users == null)
            {
                return NotFound();
            }

            return View(users);
        }

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> DeleteConfirmed(int id)
        {
            var users = await _context.Users.FindAsync(id);
            _context.Users.Remove(users);
            await _context.SaveChangesAsync();
            return RedirectToAction(nameof(Index));
        }

Follow the below steps to create a Delete View template.

Right click on the Index method and select Add View
In Templates select Delete
In Data context class select our EFCore DBContext – UserRegistrationContext
In Model class select Users
Click on Add, this will generate a new view file Delete.cshtml

@model HelloMVC.Models.EFCoreModel.Users
@{
    ViewData["Title"] = "Delete";
}

<h3>Delete</h3>
<h5>Are you sure you want to delete this User?</h5>
<div>   
    <hr />
    <dl class="row">
        <dt class = "col-sm-2">
            @Html.DisplayNameFor(model => model.FirstName)
        </dt>
        <dd class = "col-sm-10">
            @Html.DisplayFor(model => model.FirstName)
        </dd>
        <dt class = "col-sm-2">
            @Html.DisplayNameFor(model => model.LastName)
        </dt>
        <dd class = "col-sm-10">
            @Html.DisplayFor(model => model.LastName)
        </dd>
        <dt class = "col-sm-2">
            @Html.DisplayNameFor(model => model.Phone)
        </dt>
        <dd class = "col-sm-10">
            @Html.DisplayFor(model => model.Phone)
        </dd>
        <dt class = "col-sm-2">
            @Html.DisplayNameFor(model => model.Email)
        </dt>
        <dd class = "col-sm-10">
            @Html.DisplayFor(model => model.Email)
        </dd>
        <dt class = "col-sm-2">
            @Html.DisplayNameFor(model => model.Address)
        </dt>
        <dd class = "col-sm-10">
            @Html.DisplayFor(model => model.Address)
        </dd>
    </dl>
   
    <form asp-action="Delete">
        <input type="hidden" asp-for="UserId" />
        <input type="submit" value="Delete" class="btn btn-danger" /> |
        <a asp-action="Index">Back to List</a>
    </form>
</div>

Clicking on the DELETE action link will open up the Delete view of the corresponding User, we can review the details and click on the DELETE button to delete the user from the DB. This will call the POST method to delete the user.




Search Flipkart Products:
Flipkart.com

No comments: