This is What we want to Achieve
To Reach to the Index screen of above Screen Shot. Follow below link
Procedure Used
AND
Step 22 : In the Employee Controller for getting the Edit form add Edit View.
Step 25 : In EmployeeBuisnessLayer Add the following function
CREATE proc EmployeeTest_Update
@EmpID int ,
@EmpName varchar(100),
@EmpDesignation varchar(100)
as
begin
Update EmployeeTest set
EmpName=@EmpName,
EmpDesignation=@EmpDesignation
where
EmpID=@EmpID
end
create proc EmplyeeTest_Delete
@EmpID int
As
Delete from EmployeeTest where EmpID=@EmpID
[HttpGet]
public ActionResult Edit(int id)
{
EmployeeBuisnessLayer employeeBuisnessLayer = new EmployeeBuisnessLayer();
Employee employee = employeeBuisnessLayer.Employees.Single(emp
=> emp.EmpID == id);
return View(employee);
}
Step 23 : Right click and select Add View
Step 24 : In Edit.cshtml preesnt inside Employee inside Views
@model
BuisnessLayer.Employee
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Edit</title>
</head>
<body>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Employee</h4>
<hr />
@Html.ValidationSummary(true)
<div class="form-group">
@Html.HiddenFor(model => model.EmpID)
</div>
<div class="form-group">
@Html.LabelFor(model => model.EmpName, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model =>
model.EmpName)
@Html.ValidationMessageFor(model
=> model.EmpName)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.EmpDesignation, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model =>
model.EmpDesignation)
@Html.ValidationMessageFor(model
=> model.EmpDesignation)
</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>
</body>
</html>
public void Employee_Update(Employee employee)
{
String CS = ConfigurationManager.ConnectionStrings["EmplyoeeDB"].ConnectionString;
using (SqlConnection sqlcon = new SqlConnection(CS))
{
SqlCommand sqlcmd = new SqlCommand("EmployeeTest_Update", sqlcon);
sqlcmd.CommandType =
System.Data.CommandType.StoredProcedure;
sqlcmd.Parameters.AddWithValue("@EmpID",
employee.EmpID);
sqlcmd.Parameters.AddWithValue("@EmpName",
employee.EmpName);
sqlcmd.Parameters.AddWithValue("@EmpDesignation",
employee.EmpDesignation);
sqlcon.Open();
sqlcmd.ExecuteNonQuery();
}
}
public void Employee_Delete(int EmpID)
{
String CS = ConfigurationManager.ConnectionStrings["EmplyoeeDB"].ConnectionString;
using (SqlConnection sqlcon = new SqlConnection(CS))
{
SqlCommand sqlcmd = new SqlCommand("EmplyeeTest_Delete", sqlcon);
sqlcmd.CommandType = System.Data.CommandType.StoredProcedure;
sqlcmd.Parameters.AddWithValue("@EmpID",
EmpID);
sqlcon.Open();
sqlcmd.ExecuteNonQuery();
}
}
Step 26 : for posting the data add the following code in EmployeeController
[HttpPost]
public ActionResult Edit(Employee employee)
{
if (ModelState.IsValid)
{
EmployeeBuisnessLayer employeeBuisnessLayer = new EmployeeBuisnessLayer();
employeeBuisnessLayer.Employee_Update(employee);
return RedirectToAction("Index");
}
else
{
return View();
}
}
Always use Delete with HTTPPost option to prevent security flaws
[HttpPost]
public ActionResult Delete(int id)
{
EmployeeBuisnessLayer employeeBuisnessLayer = new EmployeeBuisnessLayer();
employeeBuisnessLayer.Employee_Delete(id);
return RedirectToAction("Index");
}
Step 27 : In Index.cshtml make the following change, Using Html.Beginform has been added
@foreach (var item in Model)
{
using (Html.BeginForm("Delete", "Employee", new { id = item.EmpID }))
{
<tr>
<td>
@Html.DisplayFor(modelItem
=> item.EmpID)
</td>
<td>
@Html.DisplayFor(modelItem
=> item.EmpName)
</td>
<td>
@Html.DisplayFor(modelItem
=> item.EmpDesignation)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.EmpID }) |
@Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */
}) |
<input type="submit" value="Delete" onclick="return confirm('Are you sure you wat to delete
employee: @item.EmpName')" />
</td>
</tr>
}
}
Step 28 : Adding Details functionality.
In the Employee Controller for getting the Details form add Details View.
[HttpGet]
public ActionResult Details(int id)
{
EmployeeBuisnessLayer employeeBuisnessLayer = new EmployeeBuisnessLayer();
Employee employee = employeeBuisnessLayer.Employees.Single(emp
=> emp.EmpID == id);
return View(employee);
}
Step 29 : Right click and select Add View
Step 30 : In Details.cshtml presnt inside Employee inside Views
@model
BuisnessLayer.Employee
@{
ViewBag.Title = "Details";
}
<h2>Details</h2>
<div>
<h4>Employee</h4>
<hr />
<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(model => model.EmpID)
</dt>
<dd>
@Html.DisplayFor(model => model.EmpID)
</dd>
<dt>
@Html.DisplayNameFor(model => model.EmpName)
</dt>
<dd>
@Html.DisplayFor(model => model.EmpName)
</dd>
<dt>
@Html.DisplayNameFor(model => model.EmpDesignation)
</dt>
<dd>
@Html.DisplayFor(model => model.EmpDesignation)
</dd>
</dl>
</div>
<p>
@Html.ActionLink("Edit", "Edit", new { id = Model.EmpID
}) |
@Html.ActionLink("Back
to List", "Index")
</p>
Step 30 : In Index.cshtml make the following change
@Html.ActionLink("Details", "Details", new { id = item.EmpID })
0 comments:
Post a Comment