IN Part 2 of this series I have created Model as entities
data Layer method and business Layer method means my all basic operation
already completed. Now in this article I am going to describe about how to
create controller and their Action Method and also about View.
Step 10
So Let first of all Create a Controller By Right Click on controller folder = > Add => Controller
=> Controller Name (EmployeeController ) = > Scaffolding option (MVC
controller with empty read/write action)
Step 11
Create Procedure to get all employee details
Create proc [dbo].[GetEmployeeDetails]
As
Begin
SELECT
E.Id,
E.Name,
e.salary,
d.department_id,
d.Department_Name,
d.Department_Locatiom
FROM
EmployeeDetails E
inner join
DepartmentDetails D on E.Department_id=d.Department_id
End
Step 12
No Here I am going create a View Named (Index view) to
display all Employee Details
Right Click within
Index Action method within Employee Controller => Add View => View Name (Index)
Note: View name
must same as Action Method Name
=>View Engine (Razor)
=> Check (create a strongly-typed view) =>Model class (Employee Details)
=> S scaffold temple (List) =>add
Index.cshtml (View)
@model
IEnumerable<BUSSINESSLAYER.Entites.EmployeeDetails>
@{
ViewBag.Title = "Index";
}
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.salary)
</th>
<th>
@Html.DisplayNameFor(model =>
model.employeeDepartMent.DepartMent_Name)
</th>
<th>
@Html.DisplayNameFor(model =>
model.employeeDepartMent.DepartMent_Location)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.salary)
</td>
<td>
@Html.DisplayFor(modelItem
=>item.employeeDepartMent.DepartMent_Name)
</td>
<td>
@Html.DisplayFor(modelItem
=>item.employeeDepartMent.DepartMent_Location)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.id }) |
@Html.ActionLink("Delete", "Delete", new { id=item.id })
</td>
</tr>
}
</table>
<p>
@Html.ActionLink("Create
New", "Create")
</p>
Step 14
Write Simple Code On controller to display record into index
view
using BUSSINESSLAYER.Entites;
using BUSSINESSLAYER;
public class EmployeeController : Controller
{
public ActionResult Index()
{
List<EmployeeDetails> list = (new MYBAL()).Getemployee("GetEmployeeDetails", null);
return View(list);
}
}
Step 15
Run your code and see all employee record will be shown on
index view.
Step 16
Now add create view Right
Click Within Create Action method within Employee Controller => Add View
=> View Name (Create) =>View Engine (Razor) => Check (create a
strongly-typed view ) =>Model class(Employee Details) => Scaffold temple
(create ) =>add
Note: I have also
added dropdown list into create View to insert department of employee.
@model
BUSSINESSLAYER.Entites.EmployeeDetails
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>EmployeeDetails</legend>
<div class="editor-label">
@Html.LabelFor(model =>
model.employeeDepartMent.DepartMent_Name)
</div>
<div class="editor-field">
@Html.DropDownListFor(model =>
model.employeeDepartMent.department_id , (List<SelectListItem>)ViewBag.ListofDepartment, new { style="width:250px;"
})
@Html.ValidationMessageFor(model => model.Name)
</div>
<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.salary)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.salary)
@Html.ValidationMessageFor(model => model.salary)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back
to List", "Index")
</div>
@section
Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
Step 17
Create Stored Procedure to bind Dropdown list with
Department table Record on Create View.
Create proc [dbo].[GetDepartment]
as
Begin
select
department_id,
Department_Name
from
DepartmentDetails
End
Step 18
Write method on to controller to bind dropdown list.
int departmentid = 0;
public void Bind_DepartMent(int id)
{
List<DepartmentDetails> list = (new MYBAL()).GetDepartment("GetDepartment", null);
List<SelectListItem> listitem = new List<SelectListItem>();
foreach (var item in list)
{
listitem.Add(new SelectListItem { Value
= item.department_id.ToString(), Text = item.DepartMent_Name, Selected =
item.department_id == id });
}
ViewBag.ListofDepartment = listitem;
}
Step
18
Now to Write Create Action Method for Get Result
[HttpGet]
public ActionResult Create()
{
Bind_DepartMent(departmentid);
return View();
}
Run your code and click on create Link on Index view.
Then Create view look like
Step
19
Now Again I have written a Store Procedure to insert new
employee record.
Create proc [dbo].[InsertEmployee]
(
@name varchar(50),
@Salary int,
@department_id int
)
as
Begin
INSERT INTO [dbo].[EmployeeDetails]
([Name]
,[salary]
,[Department_id])
VALUES
(@name
,@Salary,
@department_id)
End
Step 20
Write Post Action
Method to create New Employee Record on the Employee Controller.
[HttpPost]
public ActionResult Create(FormCollection collection)
{
EmployeeDetails emp = new EmployeeDetails();
emp.Name = collection["Name"];
emp.salary = Convert.ToInt32(collection["salary"]);
departmentid =
emp.employeeDepartMent.department_id = Convert.ToInt32(collection["employeeDepartMent.department_id"]);
int i = (new MYBAL()).AddNewEmployee("InsertEmployee", emp);
if (i > 0)
{
return RedirectToAction("Index");
}
else
{
Bind_DepartMent(departmentid);
return View();
}
}
Step 21
Now Create Edit View for Edit Employee Record. Right Click Within Edit Action method within Employee Controller
=> Add View => View Name (Edit ) =>View Engine (Razor) => Check
(create a strongly-typed view ) =>Model class(EmployeeDetails) =>
Scaffold temple (Edit) =>add
@model
BUSSINESSLAYER.Entites.EmployeeDetails
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>EmployeeDetails</legend>
<div class="editor-label">
@Html.LabelFor(model =>
model.employeeDepartMent.DepartMent_Name)
</div>
<div class="editor-field">
@Html.DropDownListFor(model =>
model.employeeDepartMent.department_id , (List<SelectListItem>)ViewBag.ListofDepartment, new { style="width:250px;"
})
@Html.ValidationMessageFor(model => model.Name)
</div>
<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.salary)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.salary)
@Html.ValidationMessageFor(model => model.salary)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back
to List", "Index")
</div>
@section
Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
Step 22
Now Again Create a Store Procedure to Get Single Employee
Record to fill the textbox at time of editing.
Create proc [dbo].[GetSingleEmployeesDetails]
(
@id int
)
as
Begin
SELECT E.Id,
E.Name,
e.salary,
d.department_id,
d.Department_Name,
d.Department_Locatiom
FROM
EmployeeDetails E
inner join DepartmentDetails D on
E.Department_id=d.department_id
where E.id=@id
End
Step 23
Create GET Action Method to Edit Employee Record.
[HttpGet]
public ActionResult Edit(int id)
{
SqlParameter spparmeter = new SqlParameter("@id", id);
EmployeeDetails emp = new EmployeeDetails();
emp = (new MYBAL()).GetSingleemployee("GetSingleEmployeesDetails", spparmeter);
departmentid =
emp.employeeDepartMent.department_id;
Bind_DepartMent(departmentid);
return View(emp); ;
}
Run your code and click on Edit Link button on Index then Edit view will open.
Step
23
Now Create A Store procedure to Update the employee
Record.
Create proc [dbo].[Updateemplyoee]
(
@id int,
@name varchar(50),
@salary int,
@departmentid int
)
As
begin
UPDATE [dbo].[EmployeeDetails]
SET [Name] =@name
,[salary]
= @salary
,[Department_id]
= @departmentid
WHERE [id]=@id
End
Setp 24
Create Post Action Method to Edit Purpose.
[HttpPost]
public ActionResult Edit(FormCollection Collection)
{
EmployeeDetails obj = new EmployeeDetails();
obj.id = Convert.ToInt32(Collection["id"].ToString());
obj.Name = Collection["Name"];
obj.salary = Convert.ToInt32(Collection["salary"]);
obj.employeeDepartMent.department_id
= Convert.ToInt32(Collection["employeeDepartMent.department_id"]);
int k = (new MYBAL()).UpdateEmployee("Updateemplyoee", obj);
if (k > 0)
{
return RedirectToAction("Index");
}
else
{
Bind_DepartMent(departmentid);
return View();
}
}
Note : Now Insert and Update operation will work
properly.
Step 25
Finally at last I am going to work for delete
the Existing employee record. So first of all create a Store Procedure for
delete purpose.
Create proc [dbo].[deleteEmployee]
(
@id int
)
As
Begin
DELETE FROM [dbo].[EmployeeDetails]
WHERE
[Id]=@id
End
Step 26
Create Delete Action Method.
[HttpGet]
public ActionResult Delete(int id)
{
EmployeeDetails emp = new EmployeeDetails();
emp.id = id;
int i = (new MYBAL()).DeleteEmployee("deleteEmployee", emp);
if (i > 0)
{
return RedirectToAction("Index");
}
else
{
return View();
}
}
Note : Here My CRUD Operation Finish And Final code of
Employee Controller is.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BUSSINESSLAYER.Entites;
using BUSSINESSLAYER;
using System.Data.SqlClient;
namespace CRUDOPERATION.Controllers
{
public class EmployeeController : Controller
{
public ActionResult Index()
{
List<EmployeeDetails> list = (new MYBAL()).Getemployee("GetEmployeeDetails", null);
return View(list);
}
//
// GET:
/Employee/Details/5
public ActionResult Details(int id)
{
return View();
}
//
// GET:
/Employee/Create
int departmentid = 0;
public void Bind_DepartMent(int id)
{
List<DepartmentDetails> list = (new MYBAL()).GetDepartment("GetDepartment", null);
List<SelectListItem> listitem = new List<SelectListItem>();
foreach (var item in list)
{
listitem.Add(new SelectListItem { Value
= item.department_id.ToString(), Text = item.DepartMent_Name, Selected =
item.department_id == id });
}
ViewBag.ListofDepartment =
listitem;
}
[HttpGet]
public ActionResult Create()
{
Bind_DepartMent(departmentid);
return View();
}
//
// POST:
/Employee/Create
[HttpPost]
public ActionResult Create(FormCollection collection)
{
EmployeeDetails emp = new EmployeeDetails();
emp.Name = collection["Name"];
emp.salary = Convert.ToInt32(collection["salary"]);
departmentid =
emp.employeeDepartMent.department_id = Convert.ToInt32(collection["employeeDepartMent.department_id"]);
int i = (new MYBAL()).AddNewEmployee("InsertEmployee", emp);
if (i > 0)
{
return RedirectToAction("Index");
}
else
{
Bind_DepartMent(departmentid);
return View();
}
}
//
// GET:
/Employee/Edit/5
[HttpGet]
public ActionResult Edit(int id)
{
SqlParameter spparmeter = new SqlParameter("@id", id);
EmployeeDetails emp = new EmployeeDetails();
emp = (new MYBAL()).GetSingleemployee("GetSingleEmployeesDetails", spparmeter);
departmentid =
emp.employeeDepartMent.department_id;
Bind_DepartMent(departmentid);
return View(emp); ;
}
//
// POST:
/Employee/Edit/5
[HttpPost]
public ActionResult Edit(FormCollection Collection)
{
EmployeeDetails obj = new EmployeeDetails();
obj.id = Convert.ToInt32(Collection["id"].ToString());
obj.Name = Collection["Name"];
obj.salary = Convert.ToInt32(Collection["salary"]);
obj.employeeDepartMent.department_id = Convert.ToInt32(Collection["employeeDepartMent.department_id"]);
int k = (new MYBAL()).UpdateEmployee("Updateemplyoee", obj);
if (k > 0)
{
return RedirectToAction("Index");
}
else
{
Bind_DepartMent(departmentid);
return View();
}
}
//
// GET:
/Employee/Delete/
[HttpGet]
public ActionResult Delete(int id)
{
EmployeeDetails emp = new EmployeeDetails();
emp.id = id;
int i = (new MYBAL()).DeleteEmployee("deleteEmployee", emp);
if (i > 0)
{
return RedirectToAction("Index");
}
else
{
return View();
}
}
}
}
Autor-Satya Prakash