You can visit the previous part of the article from the following links:
Create User Interface
We have added database with tables and also implemented it with Entity Data Model which has been created model classes. So, it is time to create User Interface [Views] which will used to perform user operation like here we can add data, select data, edit data and also delete data.
To add new controller, Right click on Controller folder and choose Add and then choose Add Scaffolded Item.
It will open an Add Scaffold window, Here we need to choose the Controller type. Here we need to choose MVC5 Controller with views, using Entity Framework and choose Add.
From the Add Controller window, we need to select the Model Class and Data Context Class. We can also select layout page. In the Controller Name section, we can provide the specific name for the controller "EmployeeController".
When we click on Ok. It will Scaffold and create the EmployeeController as well as Views for Employee Controller.
Note: If you don't make build before scaffolding then it will generate the error as in the following.
So, It will create EmployeeController with the following code. In this code you can see that here all the operations has been defined by default. From database instance creation to getting data, deleting data, editing data, etc everything has been defined by the code.
EmployeeController.cs
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Data.Entity;
- using System.Linq;
- using System.Net;
- using System.Web;
- using System.Web.Mvc;
- using DatabaseFirstDemo.Models;
-
- namespace DatabaseFirstDemo.Controllers
- {
- public class EmployeeController : Controller
- {
- private TestDemoEntities db = new TestDemoEntities();
-
-
- public ActionResult Index()
- {
- var employees = db.Employees.Include(e => e.Department);
- return View(employees.ToList());
- }
-
-
- public ActionResult Details(int? id)
- {
- if (id == null)
- {
- return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
- }
- Employee employee = db.Employees.Find(id);
- if (employee == null)
- {
- return HttpNotFound();
- }
- return View(employee);
- }
-
-
- public ActionResult Create()
- {
- ViewBag.DepartmentId = new SelectList(db.Departments, "DepartmentId", "DepartmentName");
- return View();
- }
-
-
-
-
- [HttpPost]
- [ValidateAntiForgeryToken]
- public ActionResult Create([Bind(Include = "Id,Name,Email,Age,Address,DepartmentId")] Employee employee)
- {
- if (ModelState.IsValid)
- {
- db.Employees.Add(employee);
- db.SaveChanges();
- return RedirectToAction("Index");
- }
-
- ViewBag.DepartmentId = new SelectList(db.Departments, "DepartmentId", "DepartmentName", employee.DepartmentId);
- return View(employee);
- }
-
-
- public ActionResult Edit(int? id)
- {
- if (id == null)
- {
- return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
- }
- Employee employee = db.Employees.Find(id);
- if (employee == null)
- {
- return HttpNotFound();
- }
- ViewBag.DepartmentId = new SelectList(db.Departments, "DepartmentId", "DepartmentName", employee.DepartmentId);
- return View(employee);
- }
-
-
-
-
- [HttpPost]
- [ValidateAntiForgeryToken]
- public ActionResult Edit([Bind(Include = "Id,Name,Email,Age,Address,DepartmentId")] Employee employee)
- {
- if (ModelState.IsValid)
- {
- db.Entry(employee).State = EntityState.Modified;
- db.SaveChanges();
- return RedirectToAction("Index");
- }
- ViewBag.DepartmentId = new SelectList(db.Departments, "DepartmentId", "DepartmentName", employee.DepartmentId);
- return View(employee);
- }
-
-
- public ActionResult Delete(int? id)
- {
- if (id == null)
- {
- return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
- }
- Employee employee = db.Employees.Find(id);
- if (employee == null)
- {
- return HttpNotFound();
- }
- return View(employee);
- }
-
-
- [HttpPost, ActionName("Delete")]
- [ValidateAntiForgeryToken]
- public ActionResult DeleteConfirmed(int id)
- {
- Employee employee = db.Employees.Find(id);
- db.Employees.Remove(employee);
- db.SaveChanges();
- return RedirectToAction("Index");
- }
-
- protected override void Dispose(bool disposing)
- {
- if (disposing)
- {
- db.Dispose();
- }
- base.Dispose(disposing);
- }
- }
- }
The following is the structure of the code after adding EmployeeController. We can see here an Employee folder has been added inside the Views and all the views such as,
Index.cshtml, edit.cshtml, etc also added.
So, this is the time of running the project. To run the project Press F5. It will open a tab in your browser as in the following.
As we have added the Employee controller. So, we need to specify the address of Employee Controller with Index page. Here you can see all the added Employee record with a
Create New Button.
Index.cshtml
- @model IEnumerable<DatabaseFirstDemo.Models.Employee>
-
- @{
- ViewBag.Title = "Index";
- Layout = "~/Views/Shared/_Layout.cshtml";
- }
-
- <h2>Index</h2>
-
- <p>
- @Html.ActionLink("Create New", "Create")
- </p>
- <table class="table">
- <tr>
- <th>
- @Html.DisplayNameFor(model => model.Name)
- </th>
- <th>
- @Html.DisplayNameFor(model => model.Email)
- </th>
- <th>
- @Html.DisplayNameFor(model => model.Age)
- </th>
- <th>
- @Html.DisplayNameFor(model => model.Address)
- </th>
- <th>
- @Html.DisplayNameFor(model => model.Department.DepartmentName)
- </th>
- <th></th>
- </tr>
-
- @foreach (var item in Model) {
- <tr>
- <td>
- @Html.DisplayFor(modelItem => item.Name)
- </td>
- <td>
- @Html.DisplayFor(modelItem => item.Email)
- </td>
- <td>
- @Html.DisplayFor(modelItem => item.Age)
- </td>
- <td>
- @Html.DisplayFor(modelItem => item.Address)
- </td>
- <td>
- @Html.DisplayFor(modelItem => item.Department.DepartmentName)
- </td>
- <td>
- @Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
- @Html.ActionLink("Details", "Details", new { id=item.Id }) |
- @Html.ActionLink("Delete", "Delete", new { id=item.Id })
- </td>
- </tr>
- }
Click on
Create New, It will redirect to create new Employee Url, Here we can add new employee record.
Create.cshtml
- @model DatabaseFirstDemo.Models.Employee
-
- @{
- ViewBag.Title = "Create";
- Layout = "~/Views/Shared/_Layout.cshtml";
- }
-
- <h2>Create</h2>
-
-
- @using (Html.BeginForm())
- {
- @Html.AntiForgeryToken()
-
- <div class="form-horizontal">
- <h4>Employee</h4>
- <hr />
- @Html.ValidationSummary(true, "", new { @class = "text-danger" })
- <div class="form-group">
- @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
- <div class="col-md-10">
- @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
- @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
- </div>
- </div>
-
- <div class="form-group">
- @Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" })
- <div class="col-md-10">
- @Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
- @Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
- </div>
- </div>
-
- <div class="form-group">
- @Html.LabelFor(model => model.Age, htmlAttributes: new { @class = "control-label col-md-2" })
- <div class="col-md-10">
- @Html.EditorFor(model => model.Age, new { htmlAttributes = new { @class = "form-control" } })
- @Html.ValidationMessageFor(model => model.Age, "", new { @class = "text-danger" })
- </div>
- </div>
-
- <div class="form-group">
- @Html.LabelFor(model => model.Address, htmlAttributes: new { @class = "control-label col-md-2" })
- <div class="col-md-10">
- @Html.EditorFor(model => model.Address, new { htmlAttributes = new { @class = "form-control" } })
- @Html.ValidationMessageFor(model => model.Address, "", new { @class = "text-danger" })
- </div>
- </div>
-
- <div class="form-group">
- @Html.LabelFor(model => model.DepartmentId, "DepartmentId", htmlAttributes: new { @class = "control-label col-md-2" })
- <div class="col-md-10">
- @Html.DropDownList("DepartmentId", null, htmlAttributes: new { @class = "form-control" })
- @Html.ValidationMessageFor(model => model.DepartmentId, "", new { @class = "text-danger" })
- </div>
- </div>
-
- <div class="form-group">
- <div class="col-md-offset-2 col-md-10">
- <input type="submit" value="Create" class="btn btn-default" />
- </div>
- </div>
- </div>
- }
-
- <div>
- @Html.ActionLink("Back to List", "Index")
- </div>
-
- @section Scripts {
- @Scripts.Render("~/bundles/jqueryval")
- }
After adding the record, we can see all the records here. Here some other links are also available to edit the record. Details of record or for deleting the record.
For deleting the record, click on
Delete link, It will ask for your confirmation to delete the record. If you will click on Delete again, then it will delete the record finally.
Delete.cshtml
- @model DatabaseFirstDemo.Models.Employee
-
- @{
- ViewBag.Title = "Delete";
- Layout = "~/Views/Shared/_Layout.cshtml";
- }
-
- <h2>Delete</h2>
-
- <h3>Are you sure you want to delete this?</h3>
- <div>
- <h4>Employee</h4>
- <hr />
- <dl class="dl-horizontal">
- <dt>
- @Html.DisplayNameFor(model => model.Name)
- </dt>
-
- <dd>
- @Html.DisplayFor(model => model.Name)
- </dd>
-
- <dt>
- @Html.DisplayNameFor(model => model.Email)
- </dt>
-
- <dd>
- @Html.DisplayFor(model => model.Email)
- </dd>
-
- <dt>
- @Html.DisplayNameFor(model => model.Age)
- </dt>
-
- <dd>
- @Html.DisplayFor(model => model.Age)
- </dd>
-
- <dt>
- @Html.DisplayNameFor(model => model.Address)
- </dt>
-
- <dd>
- @Html.DisplayFor(model => model.Address)
- </dd>
-
- <dt>
- @Html.DisplayNameFor(model => model.Department.DepartmentName)
- </dt>
-
- <dd>
- @Html.DisplayFor(model => model.Department.DepartmentName)
- </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>
To view the single employee details, we need to click on
Details link.
Details.cshtml
- @model DatabaseFirstDemo.Models.Employee
-
- @{
- ViewBag.Title = "Details";
- Layout = "~/Views/Shared/_Layout.cshtml";
- }
-
- <h2>Details</h2>
-
- <div>
- <h4>Employee</h4>
- <hr />
- <dl class="dl-horizontal">
- <dt>
- @Html.DisplayNameFor(model => model.Name)
- </dt>
-
- <dd>
- @Html.DisplayFor(model => model.Name)
- </dd>
-
- <dt>
- @Html.DisplayNameFor(model => model.Email)
- </dt>
-
- <dd>
- @Html.DisplayFor(model => model.Email)
- </dd>
-
- <dt>
- @Html.DisplayNameFor(model => model.Age)
- </dt>
-
- <dd>
- @Html.DisplayFor(model => model.Age)
- </dd>
-
- <dt>
- @Html.DisplayNameFor(model => model.Address)
- </dt>
-
- <dd>
- @Html.DisplayFor(model => model.Address)
- </dd>
-
- <dt>
- @Html.DisplayNameFor(model => model.Department.DepartmentName)
- </dt>
-
- <dd>
- @Html.DisplayFor(model => model.Department.DepartmentName)
- </dd>
-
- </dl>
- </div>
- <p>
- @Html.ActionLink("Edit", "Edit", new { id = Model.Id }) |
- @Html.ActionLink("Back to List", "Index")
- </p>
So, today we learned what is database first approach and how to implement it in ASP.NET MVC application.
Thanks for reading all the articles of this series. Hope you enjoyed it.