MVC 5
MVC 5
NET Entity
Framework
By using ado.net entity framework the system generate the following
diagram of the model of empl1 class names Correspond to the table of
the same name in database and its columns:
To accomplish this I will create a class called empl1 l and give it a property to store
a list employees :
namespace WebApplication31.Models
{
using System;
using System.Collections.Generic;
• In RouteConfig class ::
public static void
RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}"
);
routes.MapRoute(
name: "Default",
url:
"{controller}/{action}/{id}",
defaults: new { controller =
"Home", action = "Index", id =
UrlParameter.Optional }
);
}
As follow :
// GET: empl1
public ActionResult Index()
{
return View(db.empl1.ToList());
}
@model IEnumerable<WebApplication31.Models.empl1>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.fname)
</th>
<th>
@Html.DisplayNameFor(model => model.lname)
</th>
<th>
@Html.DisplayNameFor(model => model.email)
</th>
<th></th>
</tr>
</table>
• Create action :
In controller :
// GET: empl1/Create
public ActionResult Create()
{
return View();
}
// POST: empl1/Create
// To protect from overposting attacks, please enable the
specific properties you want to bind to, for
// more details see
http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include =
"id,fname,lname,email")] empl1 empl1)
{
if (ModelState.IsValid)
{
db.empl1.Add(empl1);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(empl1);
}
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>empl1</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-
danger" })
<div class="form-group">
@Html.LabelFor(model => model.fname, htmlAttributes: new
{ @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.fname, new {
htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.fname, "",
new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.lname, htmlAttributes: new
{ @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.lname, new {
htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.lname, "",
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">
<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>
• Update action :
public ActionResult Edit(int? id)
{
if (id == null)
{
return new
HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
empl1 empl1 = db.empl1.Find(id);
if (empl1 == null)
{
return HttpNotFound();
}
return View(empl1);
}
// POST: empl1/Edit/5
// To protect from overposting attacks, please enable
the specific properties you want to bind to, for
// more details see
http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include =
"id,fname,lname,email")] empl1 empl1)
{
if (ModelState.IsValid)
{
db.Entry(empl1).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(empl1);
}
@model WebApplication31.Models.empl1
@{
ViewBag.Title = "Edit";
}
<h2>Edit</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>empl1</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.id)
<div class="form-group">
@Html.LabelFor(model => model.fname, htmlAttributes: new {
@class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.fname, new { htmlAttributes
= new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.fname, "", new {
@class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.lname, htmlAttributes: new {
@class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.lname, new { htmlAttributes
= new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.lname, "", 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">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default"
/>
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
• Details page
// GET: empl1/Details/5
public ActionResult Details(int? id)
{
if (id == null)
{
return new
HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
empl1 empl1 = db.empl1.Find(id);
if (empl1 == null)
{
return HttpNotFound();
}
return View(empl1);
}
@model WebApplication31.Models.empl1
@{
ViewBag.Title = "Details";
}
<h2>Details</h2>
<div>
<h4>empl1</h4>
<hr />
<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(model => model.fname)
</dt>
<dd>
@Html.DisplayFor(model => model.fname)
</dd>
<dt>
@Html.DisplayNameFor(model => model.lname)
</dt>
<dd>
@Html.DisplayFor(model => model.lname)
</dd>
<dt>
@Html.DisplayNameFor(model => model.email)
</dt>
<dd>
@Html.DisplayFor(model => model.email)
</dd>
</dl>
</div>
<p>
@Html.ActionLink("Edit", "Edit", new { id = Model.id }) |
@Html.ActionLink("Back to List", "Index")
</p>
• Delete page
// GET: empl1/Delete/5
public ActionResult Delete(int? id)
{
if (id == null)
{
return new
HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
empl1 empl1 = db.empl1.Find(id);
if (empl1 == null)
{
return HttpNotFound();
}
return View(empl1);
}
// POST: empl1/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
empl1 empl1 = db.empl1.Find(id);
db.empl1.Remove(empl1);
db.SaveChanges();
return RedirectToAction("Index");
}
@{
ViewBag.Title = "Delete";
}
<h2>Delete</h2>
<dd>
@Html.DisplayFor(model => model.fname)
</dd>
<dt>
@Html.DisplayNameFor(model =>
model.lname)
</dt>
<dd>
@Html.DisplayFor(model => model.lname)
</dd>
<dt>
@Html.DisplayNameFor(model =>
model.email)
</dt>
<dd>
@Html.DisplayFor(model => model.email)
</dd>
</dl>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()