public void UpdateEmployee(Employee emp)
{
string conString = ConfigurationManager.ConnectionStrings["EmpContext"].ConnectionString;
int empID = emp.EmpID;
string empName = emp.Name;
string empgender = emp.Gender;
double empSalary = emp.Salary;
string empAddress = emp.Address;
string strUpdate = "update tblemp set name='" + empName + "',Gender='" + empgender + "', salary=" + empSalary + ",Address='" + empAddress + "' where EmpID="+empID;
using (SqlConnection con = new SqlConnection(conString))
{
SqlCommand cmd = new SqlCommand(strUpdate, con);
con.Open();
cmd.ExecuteNonQuery();
}
}
[HttpGet]
public ActionResult Edit(int id)
{
EmployeeBusinessLayer obj = new EmployeeBusinessLayer();
Employee emp = obj.employees.Single(x => x.EmpID == id);
return View(emp);
}
[HttpPost]
public ActionResult Edit(Employee emp)
{
if(ModelState.IsValid)
{
EmployeeBusinessLayer obj = new EmployeeBusinessLayer();
obj.UpdateEmployee(emp);
return RedirectToAction("Index");
}
return View();
}
@model DataLayer.Employee
@{
ViewBag.Title = "Edit Employee";
}
<h2>Edit Employee</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>Employee</legend>
@Html.HiddenFor(model => model.EmpID)
<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.Gender)
</div>
<div class="editor-field">
@Html.DropDownList("Gender", new List<SelectListItem>
{
new SelectListItem { Text = "Male", Value="Male" },
new SelectListItem { Text = "Female", Value="Female" }
}, "Select Gender")
@Html.ValidationMessageFor(model => model.Gender)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Address)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Address)
@Html.ValidationMessageFor(model => model.Address)
</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="Save" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("∼/bundles/jqueryval")
}
public void DeleteEmployee(int id)
{
string conString = ConfigurationManager.ConnectionStrings["EmpContext"].ConnectionString;
string strDelete = "delete tblemp where EmpID=" + id;
using (SqlConnection con = new SqlConnection(conString))
{
SqlCommand cmd = new SqlCommand(strDelete, con);
con.Open();
cmd.ExecuteNonQuery();
}
}
[HttpPost]
public ActionResult Delete(int id)
{
EmployeeBusinessLayer obj = new EmployeeBusinessLayer();
obj.DeleteEmployee(id);
return RedirectToAction("Index");
}
@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.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Gender)
</td>
<td>
@Html.DisplayFor(modelItem => item.Address)
</td>
<td>
@Html.DisplayFor(modelItem => item.Salary)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.EmpID}) |
<input type="submit" value="delete" />
</td>
</tr>
}
}
</table>
}
}