0% found this document useful (0 votes)
25 views

MVC 5

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

MVC 5

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

ASP.NET MVC and the ADO.

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 :

the class model is::

namespace WebApplication31.Models
{
using System;
using System.Collections.Generic;

public partial class empl1


{
public int id { get; set; }
public string fname { get; set; }
public string lname { get; set; }
public string email { get; set; }
}
}
The folders and files of the project as follow

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

In controller we can see all crud operations code

As follow :

• READ and display data (table)

public class empl1Controller : Controller


{
private employees2Entities db = new employees2Entities();

// GET: empl1
public ActionResult Index()
{
return View(db.empl1.ToList());
}

The view of this action :

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

@foreach (var item in Model) {


<tr>
<td>
@Html.DisplayFor(modelItem => item.fname)
</td>
<td>
@Html.DisplayFor(modelItem => item.lname)
</td>
<td>
@Html.DisplayFor(modelItem => item.email)
</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>
}

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

The create.cshtml is:


@model WebApplication31.Models.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);
}

edit.cshtml view ":

@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

The action in controller

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

The delete.cshtml page then:


@model WebApplication31.Models.empl1

@{
ViewBag.Title = "Delete";
}

<h2>Delete</h2>

<h3>Are you sure you want to delete this?</h3>


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

You might also like