Skip to content
Loading
MVC CRUD Operation With Case Cading  
  •           
  •   
  •         class="form-group">  
  •             @Html.LabelFor(model => model.MobileNumber, htmlAttributes: new { @class = "control-label col-md-2" })  
  •             class="col-md-10">  
  •                 @Html.EditorFor(model => model.MobileNumber, new { htmlAttributes = new { @class = "form-control" } })  
  •                 @Html.ValidationMessageFor(model => model.MobileNumber, "", new { @class = "text-danger" })  
  •               
  •           
  •   
  •         class="form-group">  
  •             @Html.LabelFor(model => model.DesignationID, htmlAttributes: new { @class = "control-label col-md-2" })  
  •             class="col-md-10">  
  •                 @Html.EditorFor(model => model.DesignationID, new { htmlAttributes = new { @class = "form-control" } })  
  •                 @Html.ValidationMessageFor(model => model.DesignationID, "", new { @class = "text-danger" })  
  •               
  •           
  •   
  •         class="form-group">  
  •             @Html.LabelFor(model => model.Salary, htmlAttributes: new { @class = "control-label col-md-2" })  
  •             class="col-md-10">  
  •                 @Html.EditorFor(model => model.Salary, new { htmlAttributes = new { @class = "form-control" } })  
  •                 @Html.ValidationMessageFor(model => model.Salary, "", new { @class = "text-danger" })  
  •               
  •           
  •         class="form-group">  
  •             @Html.LabelFor(model => model.SkillsID, htmlAttributes: new { @class = "control-label col-md-2" })  
  •             class="col-md-10">  
  •                 @for (int i = 0; i < Model.SkillList.Count; i++)  
  •                 {  
  •                     //string tes = "chk_" + Model.SkillList[i].SkillName;  
  •                     @Html.HiddenFor(m => Model.SkillList[i].SkillID)  
  •                     @Html.HiddenFor(m => Model.SkillList[i].SkillName)  
  •                     @Html.CheckBoxFor(m => Model.SkillList[i].Checked)  
  •                     @*"@tes" type="checkbox" value="@Model.SkillList[i].SkillName" />@Model.SkillList[i].SkillName*@  
  •                     @Html.DisplayFor(m => Model.SkillList[i].SkillName)  
  •                 }  
  •   
  •                 @Html.ValidationMessageFor(model => model.SkillsID, "", new { @class = "text-danger" })  
  •               
  •           
  •         class="form-group">  
  •             @Html.LabelFor(model => model.DepartmentID, htmlAttributes: new { @class = "control-label col-md-2" })  
  •             class="col-md-10">  
  •                 @*@Html.EditorFor(model => model.DepartmentID, new { htmlAttributes = new { @class = "form-control" } })*@  
  •                 @Html.DropDownListFor(model=>model.DepartmentID,new System.Web.Mvc.SelectList(Model.DepartmentList.OrderBy(s=>s.DepartmentName),"DepartmentID","DepartmentName",Model.DepartmentID),"-select", new { @class = "form-control", @role = "DropDown" })  
  •   
  •                 @Html.ValidationMessageFor(model => model.DepartmentID, "", new { @class = "text-danger" })  
  •               
  •           
  •   
  •         class="form-group">  
  •             @Html.LabelFor(model => model.CountryID, htmlAttributes: new { @class = "control-label col-md-2" })  
  •             class="col-md-10">  
  •                 @*@Html.EditorFor(model => model.CountryID, new { htmlAttributes = new { @class = "form-control" } })*@  
  •                 @Html.DropDownListFor(model => model.CountryID, new System.Web.Mvc.SelectList(Model.CountryList.OrderBy(s => s.CountryName), "CountryID", "CountryName", Model.CountryID), "-select-", new { @class = "form-control", @role = "DropDown" })  
  •                 @Html.ValidationMessageFor(model => model.CountryID, "", new { @class = "text-danger" })  
  •               
  •           
  •   
  •         class="form-group">  
  •             @Html.LabelFor(model => model.CityID, htmlAttributes: new { @class = "control-label col-md-2" })  
  •             class="col-md-10">  
  •                 @*@Html.EditorFor(model => model.CityID, new { htmlAttributes = new { @class = "form-control" } })*@  
  •                 @Html.DropDownListFor(model => model.CityID, new System.Web.Mvc.SelectList(Model.CityList.OrderBy(s => s.CityName), "CityID", "CityName", Model.CityID), "-select-", new { @class = "form-control", @role = "DropDown" })  
  •                 @Html.ValidationMessageFor(model => model.CityID, "", new { @class = "text-danger" })  
  •               
  •           
  •   
  •         class="form-group">  
  •             class="col-md-offset-2 col-md-10">  
  •                 "submit" value="Create" class="btn btn-default" />  
  •               
  •           
  •       
  • }  
  •   
  •   
  •     @Html.ActionLink("Back to List", "Index")  
  •   
  •   
  • "~/Scripts/jquery-1.10.2.min.js">  
  • "~/Scripts/jquery.validate.min.js">  
  • "~/Scripts/jquery.validate.unobtrusive.min.js">  
  •   
  •   
  • Edit View  
  • @model Learning1.Models.Tbl_employee  
  •   
  •   
  • @using (Html.BeginForm())  
  • {  
  •     @Html.AntiForgeryToken()  
  •   
  •     class="form-horizontal">  
  •         

    Tbl_employee

      
  •           
  •         @*@Html.ValidationSummary(true, "", new { @class = "text-danger" })*@  
  •         class="form-group">  
  •             @Html.LabelFor(model => model.EmpName, htmlAttributes: new { @class = "control-label col-md-2" })  
  •             class="col-md-10">  
  •                 @Html.EditorFor(model => model.EmpName, new { htmlAttributes = new { @class = "form-control" } })  
  •                 @Html.ValidationMessageFor(model => model.EmpName, "", new { @class = "text-danger" })  
  •               
  •           
  •   
  •   
  •         class="form-group">  
  •             @Html.LabelFor(model => model.Gender, htmlAttributes: new { @class = "control-label col-md-2" })  
  •             class="col-md-10">  
  •                 @*Male  
  •                     @Html.RadioButtonFor(model => model.Gender, "Male")  
  •                     Female  
  •                     @Html.RadioButtonFor(model => model.Gender, "Female")*@  
  •   
  •               
  •           
  •         class="form-group">  
  •             @Html.LabelFor(model => model.SkillsID, htmlAttributes: new { @class = "control-label col-md-2" })  
  •             class="col-md-10">  
  •                 @for (int i = 0; i < Model.SkillList.Count; i++)  
  •                 {  
  •                     
  •                     @Html.HiddenFor(m => Model.SkillList[i].SkillID)  
  •                     @Html.HiddenFor(m => Model.SkillList[i].SkillName)  
  •                     @Html.CheckBoxFor(m => Model.SkillList[i].Checked, htmlAttributes: new { @checked = true })  
  •                     @Html.DisplayFor(m => Model.SkillList[i].SkillName)  
  •                 }  
  •             
  •                 @Html.ValidationMessageFor(model => model.SkillsID, "", new { @class = "text-danger" })  
  •               
  •           
  •   
  •         class="form-group">  
  •             @Html.LabelFor(model => model.MobileNumber, htmlAttributes: new { @class = "control-label col-md-2" })  
  •             class="col-md-10">  
  •                 @Html.EditorFor(model => model.MobileNumber, new { htmlAttributes = new { @class = "form-control" } })  
  •                 @Html.ValidationMessageFor(model => model.MobileNumber, "", new { @class = "text-danger" })  
  •               
  •           
  •   
  •         class="form-group">  
  •             @Html.LabelFor(model => model.DesignationID, htmlAttributes: new { @class = "control-label col-md-2" })  
  •             class="col-md-10">  
  •                 @Html.EditorFor(model => model.DesignationID, new { htmlAttributes = new { @class = "form-control" } })  
  •                 @Html.ValidationMessageFor(model => model.DesignationID, "", new { @class = "text-danger" })  
  •               
  •           
  •   
  •         class="form-group">  
  •             @Html.LabelFor(model => model.Salary, htmlAttributes: new { @class = "control-label col-md-2" })  
  •             class="col-md-10">  
  •                 @Html.EditorFor(model => model.Salary, new { htmlAttributes = new { @class = "form-control" } })  
  •                 @Html.ValidationMessageFor(model => model.Salary, "", new { @class = "text-danger" })  
  •               
  •           
  •   
  •         class="form-group">  
  •             @Html.LabelFor(model => model.DepartmentID, htmlAttributes: new { @class = "control-label col-md-2" })  
  •             class="col-md-10">  
  •                 @*@Html.EditorFor(model => model.DepartmentID, new { htmlAttributes = new { @class = "form-control" } })*@  
  •                 @Html.DropDownListFor(model => model.DepartmentID, new System.Web.Mvc.SelectList(Model.DepartmentList.OrderBy(s => s.DepartmentName), "DepartmentID", "DepartmentName", Model.DepartmentID), "-select", new { @class = "form-control", @role = "DropDown" })  
  •   
  •                 @Html.ValidationMessageFor(model => model.DepartmentID, "", new { @class = "text-danger" })  
  •               
  •           
  •   
  •         class="form-group">  
  •             @Html.LabelFor(model => model.CountryID, htmlAttributes: new { @class = "control-label col-md-2" })  
  •             class="col-md-10">  
  •                 @*@Html.EditorFor(model => model.CountryID, new { htmlAttributes = new { @class = "form-control" } })*@  
  •                 @Html.DropDownListFor(model => model.CountryID, new System.Web.Mvc.SelectList(Model.CountryList.OrderBy(s => s.CountryName), "CountryID", "CountryName", Model.CountryID), "-select-", new { @class = "form-control", @role = "DropDown" })  
  •                 @Html.ValidationMessageFor(model => model.CountryID, "", new { @class = "text-danger" })  
  •               
  •           
  •   
  •         class="form-group">  
  •             @Html.LabelFor(model => model.CityID, htmlAttributes: new { @class = "control-label col-md-2" })  
  •             class="col-md-10">  
  •                 @*@Html.EditorFor(model => model.CityID, new { htmlAttributes = new { @class = "form-control" } })*@  
  •                 @Html.DropDownListFor(model => model.CityID, new System.Web.Mvc.SelectList(Model.CityList.OrderBy(s => s.CityName), "CityID", "CityName", Model.CityID), "-select-", new { @class = "form-control", @role = "DropDown" })  
  •                 @Html.ValidationMessageFor(model => model.CityID, "", new { @class = "text-danger" })  
  •               
  •           
  •   
  •         class="form-group">  
  •             class="col-md-offset-2 col-md-10">  
  •                 "submit" value="Create" class="btn btn-default" />  
  •               
  •           
  •       
  • }  
  •   
  •   
  •     @Html.ActionLink("Back to List", "List")  
  •   
  •   
  • "~/Scripts/jquery-1.10.2.min.js">  
  • "~/Scripts/jquery.validate.min.js">  
  • "~/Scripts/jquery.validate.unobtrusive.min.js">  
  •   
  •   
  • List View  
  • @model IEnumerable  
  • @using Learning1.Models;  
  • @{  
  •     ViewBag.Title = "List";  
  •     LeaningOneEntities dbEntity = new LeaningOneEntities();  
  • }  
  •   
  • List

      
  •   
  •   

  •     @Html.ActionLink("Create New", "AddNew","Employee")  
  •   
  • class="table">  
  •   
  •       
  •           
  •             @Html.DisplayNameFor(model => model.EmpName)  
  •           
  •           
  •             @Html.DisplayNameFor(model => model.Email)  
  •           
  •           
  •             @Html.DisplayNameFor(model => model.MobileNumber)  
  •           
  •           
  •             @Html.DisplayName("Designation")  
  •           
  •           
  •             @Html.DisplayNameFor(model => model.Salary)  
  •           
  •           
  •             @Html.DisplayName("Department")  
  •           
  •           
  •             @Html.DisplayName("Country")  
  •           
  •           
  •             @Html.DisplayName("City")  
  •           
  •   
  •           
  •             @Html.DisplayName("Skills")  
  •           
  •           
  •       
  •   
  • @foreach (var item in Model) {  
  •     var skill = "";  
  •       
  •           
  •             @Html.DisplayFor(modelItem => item.EmpName)  
  •           
  •           
  •             @Html.DisplayFor(modelItem => item.Email)  
  •           
  •           
  •             @Html.DisplayFor(modelItem => item.MobileNumber)  
  •           
  •           
  •             @Html.DisplayFor(modelItem => item.DesignationName)  
  •           
  •           
  •             @Html.DisplayFor(modelItem => item.Salary)  
  •           
  •           
  •             @Html.DisplayFor(modelItem => item.DepartmentName)  
  •           
  •           
  •             @Html.DisplayFor(modelItem => item.CountryName)  
  •           
  •           
  •             @Html.DisplayFor(modelItem => item.CityName)  
  •           
  •           
  •         @foreach (var i in item.empSkillSet)  
  •         {  
  •             var emplist = (from s in dbEntity.Tbl_Skills  
  •                            where s.SkillID == i.SkillID select s.SkillName).SingleOrDefault();  
  •             skill = skill + emplist  + ",";  
  •   
  •         }  
  •           
  •             @Html.DisplayFor(modelItem => skill)  
  •           
  •           
  •             @Html.ActionLink("Edit", "EditEmployee", new { EmpId = item.EmpID }) |  
  •             @Html.ActionLink("Details", "Details", new { id=item.EmpID }) |  
  •             @Html.ActionLink("Delete", "DeleteEmploye", new { EmpId = item.EmpID })  
  •           
  •       
  • }  
  •   
  •   
  •   
  •   
  • Controller   
  •   
  • using System;  
  • using System.Collections.Generic;  
  • using System.Linq;  
  • using System.Web;  
  • using System.Web.Mvc;  
  • using Learning1.Models;  
  • using System.Data.Entity;  
  •   
  • namespace Learning1.Controllers  
  • {  
  •     public class EmployeeController : Controller  
  •     {  
  •         // GET: Employee  
  •         LeaningOneEntities dbEntity = new LeaningOneEntities();  
  •         public ActionResult Index()  
  •         {  
  •             return View();  
  •         }  
  •         [HttpGet]  
  •         public ActionResult List()  
  •         {  
  •             var objList1 = (from e in dbEntity.Tbl_employee  
  •                             join d in dbEntity.Tbl_Department on e.DepartmentID equals d.DepartmentID  
  •                             join c in dbEntity.Tbl_Country on e.CountryID equals c.CountryID  
  •                             join ci in dbEntity.Tbl_City on e.CityID equals ci.CityID  
  •                             // join Emps in dbEntity.Tbl_EmpSkills on e.EmpID equals Emps.EmpID  
  •                             select new EmployeeDTO  
  •                             {  
  •                                 EmpID = e.EmpID,  
  •                                 EmpName = e.EmpName,  
  •                                 DepartmentName = d.DepartmentName,  
  •                                 Email = e.Email,  
  •                                 MobileNumber = e.MobileNumber,  
  •                                 DesignationName = e.DesignationID,  
  •                                 Salary = e.Salary,  
  •                                 CountryName = c.CountryName,  
  •                                 CityName = ci.CityName,  
  •                                 empSkillSet = dbEntity.Tbl_EmpSkills.Where(w=>w.EmpID==e.EmpID).ToList()//GetEmployeeskills(e.EmpID)  
  •                             }).ToList();  
  •             return View(objList1);  
  •         }  
  •         public ActionResult AddNew()  
  •         {  
  •             var objEmp = new Tbl_employee();  
  •             objEmp.CountryList = (from c in dbEntity.Tbl_Country select c ).ToList();  
  •             objEmp.DepartmentList = (from d in dbEntity.Tbl_Department select d).ToList();  
  •             objEmp.SkillList = (from s in dbEntity.Tbl_Skills select s).ToList();  
  •             return View(objEmp);  
  •         }  
  •   
  •         [HttpPost]  
  •         public ActionResult AddNew(Tbl_employee obj)  
  •         {  
  •             dbEntity.Tbl_employee.Add(obj);  
  •             dbEntity.SaveChanges();  
  •             List empSkills = new List();  
  •             foreach (var s in obj.SkillList.Where(s=>s.Checked))  
  •             {  
  •                 Tbl_EmpSkills objs = new Tbl_EmpSkills();  
  •                 objs.EmpID = obj.EmpID;  
  •                 objs.SkillID = s.SkillID;  
  •                 dbEntity.Tbl_EmpSkills.Add(objs);  
  •                 dbEntity.SaveChanges();  
  •             }  
  •             return RedirectToAction("List", "Employee");  
  •         }  
  •         [HttpGet]  
  •         public ActionResult EditEmployee(Int32 empID)  
  •         {  
  •             List objskill = new List();  
  •             var objEmp = dbEntity.Tbl_employee.Where(w=>w.EmpID == empID).SingleOrDefault();  
  •             objEmp.CountryList = (from c in dbEntity.Tbl_Country select c).ToList();  
  •             objEmp.DepartmentList = (from d in dbEntity.Tbl_Department select d).ToList();  
  •             var empskill = dbEntity.Tbl_EmpSkills.Where(w=>w.EmpID ==objEmp.EmpID).ToList();  
  •             objEmp.SkillList = dbEntity.Tbl_Skills.ToList();  
  •             foreach (var item in empskill)  
  •             {  
  •                 objEmp.SkillList[Convert.ToInt32(item.SkillID)-1].Checked = true;  
  •             }  
  •             objEmp.CityList = dbEntity.Tbl_City.Where(w=>w.CountryID == objEmp.CountryID).ToList();  
  •             return View(objEmp);  
  •         }  
  •         public ActionResult EditEmployee(Tbl_employee obj)  
  •         {  
  •             var EmpData = dbEntity.Tbl_employee.Where(x => x.EmpID == obj.EmpID).FirstOrDefault();  
  •             if (EmpData != null)  
  •             {  
  •                 EmpData.EmpName = obj.EmpName;  
  •                 EmpData.MobileNumber = obj.MobileNumber;  
  •                 EmpData.Salary = obj.Salary;  
  •                 EmpData.CityID = obj.CityID;  
  •                 EmpData.CountryID = obj.CountryID;  
  •                 EmpData.DesignationID = obj.DesignationID;  
  •                 EmpData.DepartmentID = obj.DepartmentID;  
  •                 dbEntity.Entry(EmpData).State = EntityState.Modified;  
  •                 dbEntity.SaveChanges();  
  •             }  
  •   
  •             var emskill = dbEntity.Tbl_EmpSkills.Where(w => w.EmpID == obj.EmpID).FirstOrDefault();  
  •             if(emskill != null)  
  •             {  
  •                 dbEntity.Entry(emskill).State = EntityState.Deleted;  
  •                 dbEntity.SaveChanges();  
  •             }  
  •             List empList = new List();  
  •             foreach (var s in obj.SkillList.Where(s => s.Checked))  
  •             {  
  •                 Tbl_EmpSkills ob = new Tbl_EmpSkills();  
  •                 ob.EmpID = obj.EmpID;  
  •                 ob.SkillID = s.SkillID;         
  •                 dbEntity.Tbl_EmpSkills.Add(ob);  
  •                 dbEntity.SaveChanges();  
  •             }  
  •             return RedirectToAction("List","Employee");  
  •         }  
  •   
  •         public ActionResult DeleteEmploye(int EmpID)  
  •         {  
  •             if (EmpID > 0)  
  •             {  
  •                 var EmpByID = dbEntity.Tbl_employee.Where(x => x.EmpID == EmpID).FirstOrDefault();  
  •                 if (EmpByID != null)  
  •                 {  
  •                     dbEntity.Entry(EmpByID).State = EntityState.Deleted;  
  •                     dbEntity.SaveChanges();  
  •                 }  
  •             }  
  •             return RedirectToAction("List", "Employee");  
  •         }  
  •         [HttpPost]  
  •         public ActionResult GetCity(string CountryID)  
  •         {  
  •             Int32 CountryID1 = Convert.ToInt32(CountryID);  
  •             var objCityList = (from c in dbEntity.Tbl_City where c.CountryID == CountryID1 select c).ToList();  
  •             return Json(objCityList);  
  •         }  
  •         public List GetEmployeeskills(Int64 EmpID)  
  •         {  
  •             var emplist = (from s in dbEntity.Tbl_Skills  
  •                            join ems in dbEntity.Tbl_EmpSkills on s.SkillID equals ems.SkillID  
  •                            where ems.EmpID == EmpID  
  •                            select //ems).ToList()  
  •                            new SkillsetDTO  
  •                            {  
  •                                SkillID = s.SkillID,  
  •                                Empskillset = s.SkillName,  
  •                                EmpID = EmpID  
  •                            }).ToList();  
  •                return emplist;  
  •         }  
  •   
  •     }  
  • }  
  •   
  • Model  
  •   
  • using System;  
  • using System.Collections.Generic;  
  • using System.Linq;  
  • using System.Web;  
  •   
  • namespace Learning1.Models  
  • {  
  •     public partial class Tbl_employee  
  •     {  
  •         public string CountryName { get; set; }  
  •         public List CountryList { get; set; }  
  •         public List CityList { get; set; }  
  •         public List DepartmentList { get; set; }  
  •   
  •         public List SkillList { get; set; }  
  •         public List empSkillSet { get; set; }  
  •   
  •            
  •   
  •         public Tbl_employee()  
  •         {  
  •             CountryList = new List();  
  •             CityList = new List();  
  •             SkillList = new List();  
  •             empSkillSet = new List();  
  •   
  •         }  
  •   
  •     }  
  •   
  •     public partial class Tbl_EmpSkills  
  •     {  
  •   
  •         public string Empskillset { get; set; }  
  •     }  
  •     public class EmployeeDTO : Tbl_employee  
  •     {  
  •         public string DepartmentName { get; set; }  
  •         public string DesignationName { get; set; }  
  •         public string CityName { get; set; }  
  •   
  •     }  
  •   
  •     public class SkillsetDTO:Tbl_EmpSkills  
  •     {  
  •   
  •     }  
  • }  
  •  

    3 Replies

    Know the answer? Post it — somebody with the same question will find it here.