namespace Evidence10.
Controllers
public class EmployeeController : Controller
private readonly EmployeeDB10Entities db=new EmployeeDB10Entities();
// GET: Employee
public ActionResult Index()
var
rmp=db.Employees.Include(e=>e.EmployeeInfoes.Select(i=>i.Skill)).OrderByDescending(x=>x.employeeI
d).ToList();
return View(rmp);
public ActionResult AddSkill(int? id)
ViewBag.Skills = new SelectList(db.Skills.ToList(), "skillId", "skillName", (id != null) ? id.ToString() :
"");
return PartialView("_addSkill");
public ActionResult Create()
return View();
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(EmployeeVM employeeVM, int[] skillId)
if(ModelState.IsValid)
Employee employee = new Employee()
{
employeeName = employeeVM.employeeName,
age = employeeVM.age,
joinDate = employeeVM.joinDate,
maritalStatus = employeeVM.maritalStatus,
};
HttpPostedFileBase file = employeeVM.pictureFile;
if(file!=null)
{
string
filePath=Path.Combine("/Images/",DateTime.Now.Ticks.ToString()+Path.GetExtension(file.FileName));
file.SaveAs(Server.MapPath(filePath));
employee.picture = filePath;
foreach (var item in skillId)
EmployeeInfo employeeInfo = new EmployeeInfo()
Employee = employee,
employeeId = employee.employeeId,
skillId = item
};
db.EmployeeInfoes.Add(employeeInfo);
db.SaveChanges();
return RedirectToAction("Index");
return View();
}
public ActionResult Edit(int? id)
Employee employee=db.Employees.First(x=>x.employeeId==id);
var skillContainer = db.EmployeeInfoes.Where(x => x.employeeId == id).ToList();
EmployeeVM employeeVM = new EmployeeVM()
employeeId = employee.employeeId,
employeeName = employee.employeeName,
age = employee.age,
joinDate = employee.joinDate,
maritalStatus = employee.maritalStatus,
picture = employee.picture,
};
if(skillContainer.Count > 0)
foreach (var item in skillContainer)
employeeVM.SkillList.Add((int)item.skillId);
return View(employeeVM);
[HttpPost]
public ActionResult Edit(EmployeeVM employeeVM, int[] skillId)
if (ModelState.IsValid)
Employee employee = new Employee()
{
employeeId=employeeVM.employeeId,
employeeName = employeeVM.employeeName,
age = employeeVM.age,
joinDate = employeeVM.joinDate,
maritalStatus = employeeVM.maritalStatus,
};
HttpPostedFileBase file = employeeVM.pictureFile;
if (file != null)
string filePath = Path.Combine("/Images/", DateTime.Now.Ticks.ToString() +
Path.GetExtension(file.FileName));
file.SaveAs(Server.MapPath(filePath));
employee.picture = filePath;
else
employee.picture = employeeVM.picture;
var esixtSkill=db.EmployeeInfoes.Where(x=>x.employeeId==employee.employeeId).ToList();
foreach (var item in esixtSkill)
db.EmployeeInfoes.Remove(item);
foreach (var item in skillId)
EmployeeInfo employeeInfo = new EmployeeInfo()
{
employeeId = employee.employeeId,
skillId = item
};
db.EmployeeInfoes.Add(employeeInfo);
db.Entry(employee).State= EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
return View();
public ActionResult Delete(int? id)
Employee employee = db.Employees.Find(id);
if (employee != null)
var delInfo = db.EmployeeInfoes.Where(x => x.employeeId == id);
db.EmployeeInfoes.RemoveRange(delInfo);
db.Employees.Remove(employee);
db.SaveChanges();
return RedirectToAction("Index");
return View();
Partial View
@model Evidence10.Models.Skill
<div class="row mb-2">
<div class="col-10">
@Html.DropDownListFor(x=>x.skillId, ViewBag.Skills as SelectList, "---Select---", new {@class="form-
select"})
</div>
<div class="col-2">
<a href="#" id="btnPlus" class="btn btn-danger"><i class="fa fa-trash"></i></a>
</div>
</div>
Create
@model Evidence10.Models.ViewModel.EmployeeVM
@{
ViewBag.Title = "Create";
@using (Html.BeginForm("Create", "Employee", FormMethod.Post, new { enctype = "multipart/form-
data" }))
@Html.AntiForgeryToken();
<div class="row mb-2">
<div class="col-6">
<h2 class="display-4"> Add New Employee</h2>
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="row mb-2">
@Html.LabelFor(x => x.employeeName, new { @class = "col-form-label col-3" })
<div class="col-7">
@Html.TextBoxFor(x => x.employeeName, new { @class = "form-control" })
</div>
<div class="col-2">
@Html.ValidationMessageFor(x => x.employeeName, "", new { @class = "text-danger" })
</div>
</div>
<div class="row mb-2">
@Html.LabelFor(x => x.age, new { @class = "col-form-label col-3" })
<div class="col-7">
@Html.TextBoxFor(x => x.age, new { @class = "form-control" })
</div>
<div class="col-2">
@Html.ValidationMessageFor(x => x.age, "", new { @class = "text-danger" })
</div>
</div>
<div class="row mb-2">
@Html.LabelFor(x => x.joinDate, new { @class = "col-form-label col-3" })
<div class="col-7">
@Html.TextBoxFor(x => x.joinDate, new { @class = "form-control",type="date" })
</div>
<div class="col-2">
@Html.ValidationMessageFor(x => x.joinDate, "", new { @class = "text-danger" })
</div>
</div>
<div class="row mb-2">
@Html.LabelFor(x => x.maritalStatus, new { @class = "col-form-label col-3" })
<div class="col-7">
@Html.CheckBoxFor(x => x.maritalStatus)
</div>
<div class="col-2">
@Html.ValidationMessageFor(x => x.maritalStatus, "", new { @class = "text-danger" })
</div>
</div>
<div class="row mb-2">
@Html.LabelFor(x => x.pictureFile, new { @class = "col-form-label col-3" })
<div class="col-7">
@Html.TextBoxFor(x => x.pictureFile, new { @class = "form-control" ,@type="file"})
</div>
<div class="col-2">
@Html.ValidationMessageFor(x => x.pictureFile, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="col-6">
<h2>Skill</h2>
<div class="d-flex justify-content-end mb-2">
<a href="#" id="btnPlus" class="btn btn-success"><i class="fa fa-plus-circle"></i></a>
</div>
<div id="container">
@Html.Action("AddSkill","Employee")
</div>
</div>
</div>
<div>
<input type="submit" name="name" value="Save Data" class="btn btn-success" />
</div>
}
Index
@model IEnumerable<Evidence10.Models.Employee>
@{
ViewBag.Title = "Index";
<h2 class="display-4">Employee Information</h2>
<div class="d-flex justify-content-end mb-2">
<a href="@Url.Action("Create","Employee")" class="btn btn-primary"><i class="fa fa-plus-
circle"></i>Add New Employee</a>
</div>
<div>
@foreach (var emp in Model)
<div class="card mb-2">
<div class="card-header d-flex justify-content-between">
<div>
<img src="@emp.picture" alt="Alternate Text" width="30" />
<label class="me-2"><b>Name : </b>@Html.DisplayFor(x => emp.employeeName)</label>
<label class="me-2"><b>Age : </b>@Html.DisplayFor(x => emp.age)</label>
<label class="me-2"><b>Join Date : </b>@Html.DisplayFor(x => emp.joinDate)</label>
<label class="me-2"><b>Marital Status : </b>@Html.DisplayFor(x =>
emp.maritalStatus)</label>
</div>
<div>
<a href="@Url.Action("Edit", "Employee", new {id=emp.employeeId})" class="btn btn-
outline-success"><i class="fa fa-pencil"></i></a>
<a href="@Url.Action("Delete", "Employee", new {id=emp.employeeId})" class="btn btn-
outline-danger"><i class="fa fa-trash"></i></a>
</div>
</div>
<div class="card-body">
@foreach (var item in emp.EmployeeInfoes)
<ul>
<li>@Html.DisplayFor(x => item.Skill.skillName)</li>
</ul>
</div>
</div>
</div>
Edit
@model Evidence10.Models.ViewModel.EmployeeVM
@{
ViewBag.Title = "Edit";
@using (Html.BeginForm("Edit", "Employee", FormMethod.Post, new { enctype = "multipart/form-data"
}))
@Html.AntiForgeryToken();
<div class="row mb-2">
<div class="col-6">
<h2 class="display-4"> Add New Employee</h2>
@Html.HiddenFor(x => x.employeeId)
@Html.HiddenFor(x => x.picture)
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="row mb-2">
@Html.LabelFor(x => x.employeeName, new { @class = "col-form-label col-3" })
<div class="col-7">
@Html.TextBoxFor(x => x.employeeName, new { @class = "form-control" })
</div>
<div class="col-2">
@Html.ValidationMessageFor(x => x.employeeName, "", new { @class = "text-danger" })
</div>
</div>
<div class="row mb-2">
@Html.LabelFor(x => x.age, new { @class = "col-form-label col-3" })
<div class="col-7">
@Html.TextBoxFor(x => x.age, new { @class = "form-control" })
</div>
<div class="col-2">
@Html.ValidationMessageFor(x => x.age, "", new { @class = "text-danger" })
</div>
</div>
<div class="row mb-2">
@Html.LabelFor(x => x.joinDate, new { @class = "col-form-label col-3" })
<div class="col-7">
@Html.TextBoxFor(x => x.joinDate, new { @class = "form-control", type = "date" })
</div>
<div class="col-2">
@Html.ValidationMessageFor(x => x.joinDate, "", new { @class = "text-danger" })
</div>
</div>
<div class="row mb-2">
@Html.LabelFor(x => x.maritalStatus, new { @class = "col-form-label col-3" })
<div class="col-7">
@Html.CheckBoxFor(x => x.maritalStatus)
</div>
<div class="col-2">
@Html.ValidationMessageFor(x => x.maritalStatus, "", new { @class = "text-danger" })
</div>
</div>
<div class="row mb-2">
@Html.LabelFor(x => x.pictureFile, new { @class = "col-form-label col-3" })
<div class="col-7">
@Html.TextBoxFor(x => x.pictureFile, new { @class = "form-control", @type = "file" })
</div>
<div class="col-2">
@Html.ValidationMessageFor(x => x.pictureFile, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="col-6">
<h2>Skill</h2>
<div class="d-flex justify-content-end mb-2">
<a href="#" id="btnPlus" class="btn btn-success"><i class="fa fa-plus-circle"></i></a>
</div>
<div id="container">
@foreach (var item in Model.SkillList)
{
@Html.Action("AddSkill", "Employee", new {id=item})
</div>
</div>
</div>
<div>
<input type="submit" name="name" value="Update Data" class="btn btn-success" />
</div>