Ado.Net Entity Data Model :

Controller.Cs Code
:
using MvcSampleApplication.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MvcSampleApplication.Controllers
{
public class EmployeeController : Controller
{
private EmployeeEntity db = new EmployeeEntity();
//
// GET: /Employee/
public ActionResult Index()
{
var empdata = db.Emp_Table.ToList();
return View(empdata);
}
//
// GET: /Employee/Details/5
public ActionResult Details(int id)
{
return View();
}
//
// GET: /Employee/Create
public ActionResult Create()
{
return View();
}
//
// POST: /Employee/Create
[HttpPost]
public ActionResult Create(Emp_Table emp)
{
try
{
// TODO: Add insert logic here
db.Emp_Table.Add(emp);
db.SaveChanges();
return RedirectToAction("Index");
}
catch
{
return View();
}
}
//
// GET: /Employee/Edit/5
public ActionResult Edit(int id)
{
Emp_Table emp = db.Emp_Table.Find(id);
return View(emp);
}
//
// POST: /Employee/Edit/5
[HttpPost]
public ActionResult Edit(int id, Emp_Table emp)
{
try
{
// TODO: Add update logic here
db.Entry(emp).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
catch
{
return View();
}
}
//
// GET: /Employee/Delete/5
public ActionResult Delete(int id)
{
Emp_Table emp = db.Emp_Table.Find(id);
return View();
}
//
// POST: /Employee/Delete/5
[HttpPost]
public ActionResult Delete(int id, FormCollection collection)
{
try
{
// TODO: Add delete logic here
Emp_Table emp = db.Emp_Table.Find(id);
db.Emp_Table.Remove(emp);
db.SaveChanges();
return RedirectToAction("Index");
}
catch
{
return View();
}
}
}
}
Index.Cshtml Code
:
@model IEnumerable<MvcSampleApplication.Models.Emp_Table>
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
WebGrid grid = new WebGrid(Model);
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create Item", "Create")
</p>
<table id="tblEmpList"></table>
<table>
<tr>
<th>Name</th>
<th>Designation</th>
<th>Location</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(ModelItem => item.Name)
</td>
<td>
@Html.DisplayFor(ModelItem => item.Designation)
</td>
<td>
@Html.DisplayFor(ModelItem => item.Location)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.Id })
</td>
<td>
@Html.ActionLink("Delete","Delete",new{id=item.Id})
</td>
</tr>
}
</table>
@grid.GetHtml(columns: new []{ grid.Column("Name"),
grid.Column("Designation"),
grid.Column("Location"),
grid.Column("Actions", format: @<text>
@Html.ActionLink("Edit", "Edit", new { id = item.Id })
|
@Html.ActionLink("Delete","Delete",new{id=item.Id})
</text>
)})
Create.Cshtml Code
:
@model MvcSampleApplication.Models.Emp_Table
@{
ViewBag.Title = "Create";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Create</h2>
<p></p>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>EmployeeEntity</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Designation)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Designation)
@Html.ValidationMessageFor(model => model.Designation)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Location)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Location)
@Html.ValidationMessageFor(model => model.Location)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
Edit.Cshtml Code :
@model MvcSampleApplication.Models.Emp_Table
@{
ViewBag.Title = "Edit";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Edit</h2>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Emp_Table</legend>
@Html.HiddenFor(model => model.Id)
<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Designation)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Designation)
@Html.ValidationMessageFor(model => model.Designation)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Location)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Location)
@Html.ValidationMessageFor(model => model.Location)
</div>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
Delete.Cshtml Code
:
@model MvcSampleApplication.Models.Emp_Table
@{
ViewBag.Title = "Delete";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Delete</h2>
<h3>Are you sure you want to delete this record?</h3>
<fieldset>
<legend>EmployeeEntity</legend>
<div class="display-label">Name</div>
<div class="display-field">
@Html.DisplayFor(model => model.Name)
</div>
<div class="display-label">Designation</div>
<div class="display-field">
@Html.DisplayFor(model => model.Designation)
</div>
<div class="display-label">Location</div>
<div class="display-field">
@Html.DisplayFor(model => model.Location)
</div>
</fieldset>
@using (Html.BeginForm()) {
<p>
<input type="submit" value="Delete" /> |
@Html.ActionLink("Back to List", "Index")
</p>
}