Singaravel Vela

Singaravel Vela

  • NA
  • 120
  • 13.6k

MVC CRUD Operation With Case Cading

Oct 31 2019 8:57 PM
Hi All,
 
I have creating MVC application Crud  opration  with Case Cading using DB First.
 
Please find the source below.
  1. Add View   
  2.   
  3. @model Learning1.Models.Tbl_employee  
  4.   
  5.   
  6. @using (Html.BeginForm())   
  7. {  
  8.     @Html.AntiForgeryToken()  
  9.       
  10.     <div class="form-horizontal">  
  11.         <h4>Tbl_employee</h4>  
  12.         <hr />  
  13.         @*@Html.ValidationSummary(true""new { @class = "text-danger" })*@  
  14.         <div class="form-group">  
  15.             @Html.LabelFor(model => model.EmpName, htmlAttributes: new { @class = "control-label col-md-2" })  
  16.             <div class="col-md-10">  
  17.                 @Html.EditorFor(model => model.EmpName, new { htmlAttributes = new { @class = "form-control" } })  
  18.                 @Html.ValidationMessageFor(model => model.EmpName, ""new { @class = "text-danger" })  
  19.             </div>  
  20.         </div>  
  21.   
  22.   
  23.         <div class="form-group">  
  24.             @Html.LabelFor(model => model.Gender, htmlAttributes: new { @class = "control-label col-md-2" })  
  25.             <div class="col-md-10">  
  26.                 @*Male  
  27.                 @Html.RadioButtonFor(model => model.Gender, "Male")  
  28.                 Female  
  29.                 @Html.RadioButtonFor(model => model.Gender, "Female")*@  
  30.                   
  31.             </div>  
  32.         </div>  
  33.         <div class="form-group">  
  34.             @Html.LabelFor(model => model.SkillsID, htmlAttributes: new { @class = "control-label col-md-2" })  
  35.             <div class="col-md-10">  
  36.                 @for (int i=0;i<Model.SkillList.Count;i++)  
  37.                 {  
  38.                     //string tes = "chk_" + Model.SkillList[i].SkillName;  
  39.                     @Html.HiddenFor(m => Model.SkillList[i].SkillID)  
  40.                     @Html.HiddenFor(m => Model.SkillList[i].SkillName)  
  41.                     @Html.CheckBoxFor(m => Model.SkillList[i].Checked)  
  42.                     @*<input id="@tes" type="checkbox" value="@Model.SkillList[i].SkillName" />@Model.SkillList[i].SkillName<br />*@  
  43.                     @Html.DisplayFor(m => Model.SkillList[i].SkillName)  
  44.                 }  
  45.   
  46.                 @Html.ValidationMessageFor(model => model.SkillsID, ""new { @class = "text-danger" })  
  47.             </div>  
  48.         </div>  
  49.   
  50.         <div class="form-group">  
  51.             @Html.LabelFor(model => model.MobileNumber, htmlAttributes: new { @class = "control-label col-md-2" })  
  52.             <div class="col-md-10">  
  53.                 @Html.EditorFor(model => model.MobileNumber, new { htmlAttributes = new { @class = "form-control" } })  
  54.                 @Html.ValidationMessageFor(model => model.MobileNumber, ""new { @class = "text-danger" })  
  55.             </div>  
  56.         </div>  
  57.   
  58.         <div class="form-group">  
  59.             @Html.LabelFor(model => model.DesignationID, htmlAttributes: new { @class = "control-label col-md-2" })  
  60.             <div class="col-md-10">  
  61.                 @Html.EditorFor(model => model.DesignationID, new { htmlAttributes = new { @class = "form-control" } })  
  62.                 @Html.ValidationMessageFor(model => model.DesignationID, ""new { @class = "text-danger" })  
  63.             </div>  
  64.         </div>  
  65.   
  66.         <div class="form-group">  
  67.             @Html.LabelFor(model => model.Salary, htmlAttributes: new { @class = "control-label col-md-2" })  
  68.             <div class="col-md-10">  
  69.                 @Html.EditorFor(model => model.Salary, new { htmlAttributes = new { @class = "form-control" } })  
  70.                 @Html.ValidationMessageFor(model => model.Salary, ""new { @class = "text-danger" })  
  71.             </div>  
  72.         </div>  
  73.         <div class="form-group">  
  74.             @Html.LabelFor(model => model.SkillsID, htmlAttributes: new { @class = "control-label col-md-2" })  
  75.             <div class="col-md-10">  
  76.                 @for (int i = 0; i < Model.SkillList.Count; i++)  
  77.                 {  
  78.                     //string tes = "chk_" + Model.SkillList[i].SkillName;  
  79.                     @Html.HiddenFor(m => Model.SkillList[i].SkillID)  
  80.                     @Html.HiddenFor(m => Model.SkillList[i].SkillName)  
  81.                     @Html.CheckBoxFor(m => Model.SkillList[i].Checked)  
  82.                     @*<input id="@tes" type="checkbox" value="@Model.SkillList[i].SkillName" />@Model.SkillList[i].SkillName<br />*@  
  83.                     @Html.DisplayFor(m => Model.SkillList[i].SkillName)  
  84.                 }  
  85.   
  86.                 @Html.ValidationMessageFor(model => model.SkillsID, ""new { @class = "text-danger" })  
  87.             </div>  
  88.         </div>  
  89.         <div class="form-group">  
  90.             @Html.LabelFor(model => model.DepartmentID, htmlAttributes: new { @class = "control-label col-md-2" })  
  91.             <div class="col-md-10">  
  92.                 @*@Html.EditorFor(model => model.DepartmentID, new { htmlAttributes = new { @class = "form-control" } })*@  
  93.                 @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" })  
  94.   
  95.                 @Html.ValidationMessageFor(model => model.DepartmentID, ""new { @class = "text-danger" })  
  96.             </div>  
  97.         </div>  
  98.   
  99.         <div class="form-group">  
  100.             @Html.LabelFor(model => model.CountryID, htmlAttributes: new { @class = "control-label col-md-2" })  
  101.             <div class="col-md-10">  
  102.                 @*@Html.EditorFor(model => model.CountryID, new { htmlAttributes = new { @class = "form-control" } })*@  
  103.                 @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" })  
  104.                 @Html.ValidationMessageFor(model => model.CountryID, ""new { @class = "text-danger" })  
  105.             </div>  
  106.         </div>  
  107.   
  108.         <div class="form-group">  
  109.             @Html.LabelFor(model => model.CityID, htmlAttributes: new { @class = "control-label col-md-2" })  
  110.             <div class="col-md-10">  
  111.                 @*@Html.EditorFor(model => model.CityID, new { htmlAttributes = new { @class = "form-control" } })*@  
  112.                 @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" })  
  113.                 @Html.ValidationMessageFor(model => model.CityID, ""new { @class = "text-danger" })  
  114.             </div>  
  115.         </div>  
  116.   
  117.         <div class="form-group">  
  118.             <div class="col-md-offset-2 col-md-10">  
  119.                 <input type="submit" value="Create" class="btn btn-default" />  
  120.             </div>  
  121.         </div>  
  122.     </div>  
  123. }  
  124.   
  125. <div>  
  126.     @Html.ActionLink("Back to List""Index")  
  127. </div>  
  128.   
  129. <script src="~/Scripts/jquery-1.10.2.min.js"></script>  
  130. <script src="~/Scripts/jquery.validate.min.js"></script>  
  131. <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>  
  132. <script>  
  133.     $(document).ready(function () {  
  134.         $("#CountryID").change(function () {  
  135.            
  136.             $.ajax({  
  137.                 type: "POST",  
  138.                 url: '@Url.Action("GetCity", "Employee")',  
  139.                // contentType: "application/json; charset=utf-8",  
  140.                 data: { CountryID: $(this).val() },  
  141.                 dataType: "json",  
  142.                 success: function (response) {  
  143.                     let options = '';  
  144.                     options += '<option value=0>-Select-</option>';  
  145.                     $.each(response, function (i, item) {  
  146.                         options += '<option value='+item.CityID+'>'+item.CityName+'</option>';  
  147.                           
  148.                     })  
  149.                     ;  
  150.                     $('#CityID').html(options);  
  151.                 },  
  152.                 error: function (error) {  
  153.                     debugger  
  154.                 },  
  155.             });  
  156.         });  
  157.     });  
  158. </script>  
  159.   
  160. Edit View  
  161. @model Learning1.Models.Tbl_employee  
  162.   
  163.   
  164. @using (Html.BeginForm())  
  165. {  
  166.     @Html.AntiForgeryToken()  
  167.   
  168.     <div class="form-horizontal">  
  169.         <h4>Tbl_employee</h4>  
  170.         <hr />  
  171.         @*@Html.ValidationSummary(true""new { @class = "text-danger" })*@  
  172.         <div class="form-group">  
  173.             @Html.LabelFor(model => model.EmpName, htmlAttributes: new { @class = "control-label col-md-2" })  
  174.             <div class="col-md-10">  
  175.                 @Html.EditorFor(model => model.EmpName, new { htmlAttributes = new { @class = "form-control" } })  
  176.                 @Html.ValidationMessageFor(model => model.EmpName, ""new { @class = "text-danger" })  
  177.             </div>  
  178.         </div>  
  179.   
  180.   
  181.         <div class="form-group">  
  182.             @Html.LabelFor(model => model.Gender, htmlAttributes: new { @class = "control-label col-md-2" })  
  183.             <div class="col-md-10">  
  184.                 @*Male  
  185.                     @Html.RadioButtonFor(model => model.Gender, "Male")  
  186.                     Female  
  187.                     @Html.RadioButtonFor(model => model.Gender, "Female")*@  
  188.   
  189.             </div>  
  190.         </div>  
  191.         <div class="form-group">  
  192.             @Html.LabelFor(model => model.SkillsID, htmlAttributes: new { @class = "control-label col-md-2" })  
  193.             <div class="col-md-10">  
  194.                 @for (int i = 0; i < Model.SkillList.Count; i++)  
  195.                 {  
  196.                     
  197.                     @Html.HiddenFor(m => Model.SkillList[i].SkillID)  
  198.                     @Html.HiddenFor(m => Model.SkillList[i].SkillName)  
  199.                     @Html.CheckBoxFor(m => Model.SkillList[i].Checked, htmlAttributes: new { @checked = true })  
  200.                     @Html.DisplayFor(m => Model.SkillList[i].SkillName)  
  201.                 }  
  202.             
  203.                 @Html.ValidationMessageFor(model => model.SkillsID, ""new { @class = "text-danger" })  
  204.             </div>  
  205.         </div>  
  206.   
  207.         <div class="form-group">  
  208.             @Html.LabelFor(model => model.MobileNumber, htmlAttributes: new { @class = "control-label col-md-2" })  
  209.             <div class="col-md-10">  
  210.                 @Html.EditorFor(model => model.MobileNumber, new { htmlAttributes = new { @class = "form-control" } })  
  211.                 @Html.ValidationMessageFor(model => model.MobileNumber, ""new { @class = "text-danger" })  
  212.             </div>  
  213.         </div>  
  214.   
  215.         <div class="form-group">  
  216.             @Html.LabelFor(model => model.DesignationID, htmlAttributes: new { @class = "control-label col-md-2" })  
  217.             <div class="col-md-10">  
  218.                 @Html.EditorFor(model => model.DesignationID, new { htmlAttributes = new { @class = "form-control" } })  
  219.                 @Html.ValidationMessageFor(model => model.DesignationID, ""new { @class = "text-danger" })  
  220.             </div>  
  221.         </div>  
  222.   
  223.         <div class="form-group">  
  224.             @Html.LabelFor(model => model.Salary, htmlAttributes: new { @class = "control-label col-md-2" })  
  225.             <div class="col-md-10">  
  226.                 @Html.EditorFor(model => model.Salary, new { htmlAttributes = new { @class = "form-control" } })  
  227.                 @Html.ValidationMessageFor(model => model.Salary, ""new { @class = "text-danger" })  
  228.             </div>  
  229.         </div>  
  230.   
  231.         <div class="form-group">  
  232.             @Html.LabelFor(model => model.DepartmentID, htmlAttributes: new { @class = "control-label col-md-2" })  
  233.             <div class="col-md-10">  
  234.                 @*@Html.EditorFor(model => model.DepartmentID, new { htmlAttributes = new { @class = "form-control" } })*@  
  235.                 @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" })  
  236.   
  237.                 @Html.ValidationMessageFor(model => model.DepartmentID, ""new { @class = "text-danger" })  
  238.             </div>  
  239.         </div>  
  240.   
  241.         <div class="form-group">  
  242.             @Html.LabelFor(model => model.CountryID, htmlAttributes: new { @class = "control-label col-md-2" })  
  243.             <div class="col-md-10">  
  244.                 @*@Html.EditorFor(model => model.CountryID, new { htmlAttributes = new { @class = "form-control" } })*@  
  245.                 @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" })  
  246.                 @Html.ValidationMessageFor(model => model.CountryID, ""new { @class = "text-danger" })  
  247.             </div>  
  248.         </div>  
  249.   
  250.         <div class="form-group">  
  251.             @Html.LabelFor(model => model.CityID, htmlAttributes: new { @class = "control-label col-md-2" })  
  252.             <div class="col-md-10">  
  253.                 @*@Html.EditorFor(model => model.CityID, new { htmlAttributes = new { @class = "form-control" } })*@  
  254.                 @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" })  
  255.                 @Html.ValidationMessageFor(model => model.CityID, ""new { @class = "text-danger" })  
  256.             </div>  
  257.         </div>  
  258.   
  259.         <div class="form-group">  
  260.             <div class="col-md-offset-2 col-md-10">  
  261.                 <input type="submit" value="Create" class="btn btn-default" />  
  262.             </div>  
  263.         </div>  
  264.     </div>  
  265. }  
  266.   
  267. <div>  
  268.     @Html.ActionLink("Back to List""List")  
  269. </div>  
  270.   
  271. <script src="~/Scripts/jquery-1.10.2.min.js"></script>  
  272. <script src="~/Scripts/jquery.validate.min.js"></script>  
  273. <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>  
  274. <script>  
  275.     $(document).ready(function () {  
  276.         $("#CountryID").change(function () {  
  277.   
  278.             $.ajax({  
  279.                 type: "POST",  
  280.                 url: '@Url.Action("GetCity", "Employee")',  
  281.                // contentType: "application/json; charset=utf-8",  
  282.                 data: { CountryID: $(this).val() },  
  283.                 dataType: "json",  
  284.                 success: function (response) {  
  285.                     let options = '';  
  286.                     options += '<option value=0>-Select-</option>';  
  287.                     $.each(response, function (i, item) {  
  288.                         options += '<option value='+item.CityID+'>'+item.CityName+'</option>';  
  289.   
  290.                     })  
  291.                     ;  
  292.                     $('#CityID').html(options);  
  293.                 },  
  294.                 error: function (error) {  
  295.                     debugger  
  296.                 },  
  297.             });  
  298.         });  
  299.     });  
  300. </script>  
  301.   
  302. List View  
  303. @model IEnumerable<Learning1.Models.EmployeeDTO>  
  304. @using Learning1.Models;  
  305. @{  
  306.     ViewBag.Title = "List";  
  307.     LeaningOneEntities dbEntity = new LeaningOneEntities();  
  308. }  
  309.   
  310. <h2>List</h2>  
  311.   
  312. <p>  
  313.     @Html.ActionLink("Create New""AddNew","Employee")  
  314. </p>  
  315. <table class="table">  
  316.   
  317.     <tr>  
  318.         <th>  
  319.             @Html.DisplayNameFor(model => model.EmpName)  
  320.         </th>  
  321.         <th>  
  322.             @Html.DisplayNameFor(model => model.Email)  
  323.         </th>  
  324.         <th>  
  325.             @Html.DisplayNameFor(model => model.MobileNumber)  
  326.         </th>  
  327.         <th>  
  328.             @Html.DisplayName("Designation")  
  329.         </th>  
  330.         <th>  
  331.             @Html.DisplayNameFor(model => model.Salary)  
  332.         </th>  
  333.         <th>  
  334.             @Html.DisplayName("Department")  
  335.         </th>  
  336.         <th>  
  337.             @Html.DisplayName("Country")  
  338.         </th>  
  339.         <th>  
  340.             @Html.DisplayName("City")  
  341.         </th>  
  342.   
  343.         <th>  
  344.             @Html.DisplayName("Skills")  
  345.         </th>  
  346.         <th></th>  
  347.     </tr>  
  348.   
  349. @foreach (var item in Model) {  
  350.     var skill = "";  
  351.     <tr>  
  352.         <td>  
  353.             @Html.DisplayFor(modelItem => item.EmpName)  
  354.         </td>  
  355.         <td>  
  356.             @Html.DisplayFor(modelItem => item.Email)  
  357.         </td>  
  358.         <td>  
  359.             @Html.DisplayFor(modelItem => item.MobileNumber)  
  360.         </td>  
  361.         <td>  
  362.             @Html.DisplayFor(modelItem => item.DesignationName)  
  363.         </td>  
  364.         <td>  
  365.             @Html.DisplayFor(modelItem => item.Salary)  
  366.         </td>  
  367.         <td>  
  368.             @Html.DisplayFor(modelItem => item.DepartmentName)  
  369.         </td>  
  370.         <td>  
  371.             @Html.DisplayFor(modelItem => item.CountryName)  
  372.         </td>  
  373.         <td>  
  374.             @Html.DisplayFor(modelItem => item.CityName)  
  375.         </td>  
  376.           
  377.         @foreach (var i in item.empSkillSet)  
  378.         {  
  379.             var emplist = (from s in dbEntity.Tbl_Skills  
  380.                            where s.SkillID == i.SkillID select s.SkillName).SingleOrDefault();  
  381.             skill = skill + emplist  + ",";  
  382.   
  383.         }  
  384.         <td>  
  385.             @Html.DisplayFor(modelItem => skill)  
  386.         </td>  
  387.         <td>  
  388.             @Html.ActionLink("Edit""EditEmployee"new { EmpId = item.EmpID }) |  
  389.             @Html.ActionLink("Details""Details"new { id=item.EmpID }) |  
  390.             @Html.ActionLink("Delete""DeleteEmploye"new { EmpId = item.EmpID })  
  391.         </td>  
  392.     </tr>  
  393. }  
  394.   
  395. </table>  
  396.   
  397.   
  398. Controller   
  399.   
  400. using System;  
  401. using System.Collections.Generic;  
  402. using System.Linq;  
  403. using System.Web;  
  404. using System.Web.Mvc;  
  405. using Learning1.Models;  
  406. using System.Data.Entity;  
  407.   
  408. namespace Learning1.Controllers  
  409. {  
  410.     public class EmployeeController : Controller  
  411.     {  
  412.         // GET: Employee  
  413.         LeaningOneEntities dbEntity = new LeaningOneEntities();  
  414.         public ActionResult Index()  
  415.         {  
  416.             return View();  
  417.         }  
  418.         [HttpGet]  
  419.         public ActionResult List()  
  420.         {  
  421.             var objList1 = (from e in dbEntity.Tbl_employee  
  422.                             join d in dbEntity.Tbl_Department on e.DepartmentID equals d.DepartmentID  
  423.                             join c in dbEntity.Tbl_Country on e.CountryID equals c.CountryID  
  424.                             join ci in dbEntity.Tbl_City on e.CityID equals ci.CityID  
  425.                             // join Emps in dbEntity.Tbl_EmpSkills on e.EmpID equals Emps.EmpID  
  426.                             select new EmployeeDTO  
  427.                             {  
  428.                                 EmpID = e.EmpID,  
  429.                                 EmpName = e.EmpName,  
  430.                                 DepartmentName = d.DepartmentName,  
  431.                                 Email = e.Email,  
  432.                                 MobileNumber = e.MobileNumber,  
  433.                                 DesignationName = e.DesignationID,  
  434.                                 Salary = e.Salary,  
  435.                                 CountryName = c.CountryName,  
  436.                                 CityName = ci.CityName,  
  437.                                 empSkillSet = dbEntity.Tbl_EmpSkills.Where(w=>w.EmpID==e.EmpID).ToList()//GetEmployeeskills(e.EmpID)  
  438.                             }).ToList();  
  439.             return View(objList1);  
  440.         }  
  441.         public ActionResult AddNew()  
  442.         {  
  443.             var objEmp = new Tbl_employee();  
  444.             objEmp.CountryList = (from c in dbEntity.Tbl_Country select c ).ToList();  
  445.             objEmp.DepartmentList = (from d in dbEntity.Tbl_Department select d).ToList();  
  446.             objEmp.SkillList = (from s in dbEntity.Tbl_Skills select s).ToList();  
  447.             return View(objEmp);  
  448.         }  
  449.   
  450.         [HttpPost]  
  451.         public ActionResult AddNew(Tbl_employee obj)  
  452.         {  
  453.             dbEntity.Tbl_employee.Add(obj);  
  454.             dbEntity.SaveChanges();  
  455.             List<Tbl_EmpSkills> empSkills = new List<Tbl_EmpSkills>();  
  456.             foreach (var s in obj.SkillList.Where(s=>s.Checked))  
  457.             {  
  458.                 Tbl_EmpSkills objs = new Tbl_EmpSkills();  
  459.                 objs.EmpID = obj.EmpID;  
  460.                 objs.SkillID = s.SkillID;  
  461.                 dbEntity.Tbl_EmpSkills.Add(objs);  
  462.                 dbEntity.SaveChanges();  
  463.             }  
  464.             return RedirectToAction("List""Employee");  
  465.         }  
  466.         [HttpGet]  
  467.         public ActionResult EditEmployee(Int32 empID)  
  468.         {  
  469.             List<Tbl_Skills> objskill = new List<Tbl_Skills>();  
  470.             var objEmp = dbEntity.Tbl_employee.Where(w=>w.EmpID == empID).SingleOrDefault();  
  471.             objEmp.CountryList = (from c in dbEntity.Tbl_Country select c).ToList();  
  472.             objEmp.DepartmentList = (from d in dbEntity.Tbl_Department select d).ToList();  
  473.             var empskill = dbEntity.Tbl_EmpSkills.Where(w=>w.EmpID ==objEmp.EmpID).ToList();  
  474.             objEmp.SkillList = dbEntity.Tbl_Skills.ToList();  
  475.             foreach (var item in empskill)  
  476.             {  
  477.                 objEmp.SkillList[Convert.ToInt32(item.SkillID)-1].Checked = true;  
  478.             }  
  479.             objEmp.CityList = dbEntity.Tbl_City.Where(w=>w.CountryID == objEmp.CountryID).ToList();  
  480.             return View(objEmp);  
  481.         }  
  482.         public ActionResult EditEmployee(Tbl_employee obj)  
  483.         {  
  484.             var EmpData = dbEntity.Tbl_employee.Where(x => x.EmpID == obj.EmpID).FirstOrDefault();  
  485.             if (EmpData != null)  
  486.             {  
  487.                 EmpData.EmpName = obj.EmpName;  
  488.                 EmpData.MobileNumber = obj.MobileNumber;  
  489.                 EmpData.Salary = obj.Salary;  
  490.                 EmpData.CityID = obj.CityID;  
  491.                 EmpData.CountryID = obj.CountryID;  
  492.                 EmpData.DesignationID = obj.DesignationID;  
  493.                 EmpData.DepartmentID = obj.DepartmentID;  
  494.                 dbEntity.Entry(EmpData).State = EntityState.Modified;  
  495.                 dbEntity.SaveChanges();  
  496.             }  
  497.   
  498.             var emskill = dbEntity.Tbl_EmpSkills.Where(w => w.EmpID == obj.EmpID).FirstOrDefault();  
  499.             if(emskill != null)  
  500.             {  
  501.                 dbEntity.Entry(emskill).State = EntityState.Deleted;  
  502.                 dbEntity.SaveChanges();  
  503.             }  
  504.             List<Tbl_EmpSkills> empList = new List<Tbl_EmpSkills>();  
  505.             foreach (var s in obj.SkillList.Where(s => s.Checked))  
  506.             {  
  507.                 Tbl_EmpSkills ob = new Tbl_EmpSkills();  
  508.                 ob.EmpID = obj.EmpID;  
  509.                 ob.SkillID = s.SkillID;         
  510.                 dbEntity.Tbl_EmpSkills.Add(ob);  
  511.                 dbEntity.SaveChanges();  
  512.             }  
  513.             return RedirectToAction("List","Employee");  
  514.         }  
  515.   
  516.         public ActionResult DeleteEmploye(int EmpID)  
  517.         {  
  518.             if (EmpID > 0)  
  519.             {  
  520.                 var EmpByID = dbEntity.Tbl_employee.Where(x => x.EmpID == EmpID).FirstOrDefault();  
  521.                 if (EmpByID != null)  
  522.                 {  
  523.                     dbEntity.Entry(EmpByID).State = EntityState.Deleted;  
  524.                     dbEntity.SaveChanges();  
  525.                 }  
  526.             }  
  527.             return RedirectToAction("List""Employee");  
  528.         }  
  529.         [HttpPost]  
  530.         public ActionResult GetCity(string CountryID)  
  531.         {  
  532.             Int32 CountryID1 = Convert.ToInt32(CountryID);  
  533.             var objCityList = (from c in dbEntity.Tbl_City where c.CountryID == CountryID1 select c).ToList();  
  534.             return Json(objCityList);  
  535.         }  
  536.         public List<SkillsetDTO> GetEmployeeskills(Int64 EmpID)  
  537.         {  
  538.             var emplist = (from s in dbEntity.Tbl_Skills  
  539.                            join ems in dbEntity.Tbl_EmpSkills on s.SkillID equals ems.SkillID  
  540.                            where ems.EmpID == EmpID  
  541.                            select //ems).ToList()  
  542.                            new SkillsetDTO  
  543.                            {  
  544.                                SkillID = s.SkillID,  
  545.                                Empskillset = s.SkillName,  
  546.                                EmpID = EmpID  
  547.                            }).ToList();  
  548.                return emplist;  
  549.         }  
  550.   
  551.     }  
  552. }  
  553.   
  554. Model  
  555.   
  556. using System;  
  557. using System.Collections.Generic;  
  558. using System.Linq;  
  559. using System.Web;  
  560.   
  561. namespace Learning1.Models  
  562. {  
  563.     public partial class Tbl_employee  
  564.     {  
  565.         public string CountryName { getset; }  
  566.         public List<Tbl_Country> CountryList { getset; }  
  567.         public List<Tbl_City> CityList { getset; }  
  568.         public List<Tbl_Department> DepartmentList { getset; }  
  569.   
  570.         public List<Tbl_Skills> SkillList { getset; }  
  571.         public List<Tbl_EmpSkills> empSkillSet { getset; }  
  572.   
  573.            
  574.   
  575.         public Tbl_employee()  
  576.         {  
  577.             CountryList = new List<Tbl_Country>();  
  578.             CityList = new List<Tbl_City>();  
  579.             SkillList = new List<Tbl_Skills>();  
  580.             empSkillSet = new List<Tbl_EmpSkills>();  
  581.   
  582.         }  
  583.   
  584.     }  
  585.   
  586.     public partial class Tbl_EmpSkills  
  587.     {  
  588.   
  589.         public string Empskillset { getset; }  
  590.     }  
  591.     public class EmployeeDTO : Tbl_employee  
  592.     {  
  593.         public string DepartmentName { getset; }  
  594.         public string DesignationName { getset; }  
  595.         public string CityName { getset; }  
  596.   
  597.     }  
  598.   
  599.     public class SkillsetDTO:Tbl_EmpSkills  
  600.     {  
  601.   
  602.     }  
  603. }  
 

Answers (3)