Hi All,
I have creating MVC application Crud opration with Case Cading using DB First.
Please find the source below.
- Add View
-
- @model Learning1.Models.Tbl_employee
-
-
- @using (Html.BeginForm())
- {
- @Html.AntiForgeryToken()
-
- <div class="form-horizontal">
- <h4>Tbl_employee</h4>
- <hr />
- @*@Html.ValidationSummary(true, "", new { @class = "text-danger" })*@
- <div class="form-group">
- @Html.LabelFor(model => model.EmpName, htmlAttributes: new { @class = "control-label col-md-2" })
- <div class="col-md-10">
- @Html.EditorFor(model => model.EmpName, new { htmlAttributes = new { @class = "form-control" } })
- @Html.ValidationMessageFor(model => model.EmpName, "", new { @class = "text-danger" })
- </div>
- </div>
-
-
- <div class="form-group">
- @Html.LabelFor(model => model.Gender, htmlAttributes: new { @class = "control-label col-md-2" })
- <div class="col-md-10">
- @*Male
- @Html.RadioButtonFor(model => model.Gender, "Male")
- Female
- @Html.RadioButtonFor(model => model.Gender, "Female")*@
-
- </div>
- </div>
- <div class="form-group">
- @Html.LabelFor(model => model.SkillsID, htmlAttributes: new { @class = "control-label col-md-2" })
- <div 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)
- @*<input id="@tes" type="checkbox" value="@Model.SkillList[i].SkillName" />@Model.SkillList[i].SkillName<br />*@
- @Html.DisplayFor(m => Model.SkillList[i].SkillName)
- }
-
- @Html.ValidationMessageFor(model => model.SkillsID, "", new { @class = "text-danger" })
- </div>
- </div>
-
- <div class="form-group">
- @Html.LabelFor(model => model.MobileNumber, htmlAttributes: new { @class = "control-label col-md-2" })
- <div class="col-md-10">
- @Html.EditorFor(model => model.MobileNumber, new { htmlAttributes = new { @class = "form-control" } })
- @Html.ValidationMessageFor(model => model.MobileNumber, "", new { @class = "text-danger" })
- </div>
- </div>
-
- <div class="form-group">
- @Html.LabelFor(model => model.DesignationID, htmlAttributes: new { @class = "control-label col-md-2" })
- <div class="col-md-10">
- @Html.EditorFor(model => model.DesignationID, new { htmlAttributes = new { @class = "form-control" } })
- @Html.ValidationMessageFor(model => model.DesignationID, "", new { @class = "text-danger" })
- </div>
- </div>
-
- <div class="form-group">
- @Html.LabelFor(model => model.Salary, htmlAttributes: new { @class = "control-label col-md-2" })
- <div class="col-md-10">
- @Html.EditorFor(model => model.Salary, new { htmlAttributes = new { @class = "form-control" } })
- @Html.ValidationMessageFor(model => model.Salary, "", new { @class = "text-danger" })
- </div>
- </div>
- <div class="form-group">
- @Html.LabelFor(model => model.SkillsID, htmlAttributes: new { @class = "control-label col-md-2" })
- <div 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)
- @*<input id="@tes" type="checkbox" value="@Model.SkillList[i].SkillName" />@Model.SkillList[i].SkillName<br />*@
- @Html.DisplayFor(m => Model.SkillList[i].SkillName)
- }
-
- @Html.ValidationMessageFor(model => model.SkillsID, "", new { @class = "text-danger" })
- </div>
- </div>
- <div class="form-group">
- @Html.LabelFor(model => model.DepartmentID, htmlAttributes: new { @class = "control-label col-md-2" })
- <div 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" })
- </div>
- </div>
-
- <div class="form-group">
- @Html.LabelFor(model => model.CountryID, htmlAttributes: new { @class = "control-label col-md-2" })
- <div 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" })
- </div>
- </div>
-
- <div class="form-group">
- @Html.LabelFor(model => model.CityID, htmlAttributes: new { @class = "control-label col-md-2" })
- <div 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" })
- </div>
- </div>
-
- <div class="form-group">
- <div class="col-md-offset-2 col-md-10">
- <input type="submit" value="Create" class="btn btn-default" />
- </div>
- </div>
- </div>
- }
-
- <div>
- @Html.ActionLink("Back to List", "Index")
- </div>
-
- <script src="~/Scripts/jquery-1.10.2.min.js"></script>
- <script src="~/Scripts/jquery.validate.min.js"></script>
- <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
- <script>
- $(document).ready(function () {
- $("#CountryID").change(function () {
-
- $.ajax({
- type: "POST",
- url: '@Url.Action("GetCity", "Employee")',
-
- data: { CountryID: $(this).val() },
- dataType: "json",
- success: function (response) {
- let options = '';
- options += '<option value=0>-Select-</option>';
- $.each(response, function (i, item) {
- options += '<option value='+item.CityID+'>'+item.CityName+'</option>';
-
- })
- ;
- $('#CityID').html(options);
- },
- error: function (error) {
- debugger
- },
- });
- });
- });
- </script>
-
- Edit View
- @model Learning1.Models.Tbl_employee
-
-
- @using (Html.BeginForm())
- {
- @Html.AntiForgeryToken()
-
- <div class="form-horizontal">
- <h4>Tbl_employee</h4>
- <hr />
- @*@Html.ValidationSummary(true, "", new { @class = "text-danger" })*@
- <div class="form-group">
- @Html.LabelFor(model => model.EmpName, htmlAttributes: new { @class = "control-label col-md-2" })
- <div class="col-md-10">
- @Html.EditorFor(model => model.EmpName, new { htmlAttributes = new { @class = "form-control" } })
- @Html.ValidationMessageFor(model => model.EmpName, "", new { @class = "text-danger" })
- </div>
- </div>
-
-
- <div class="form-group">
- @Html.LabelFor(model => model.Gender, htmlAttributes: new { @class = "control-label col-md-2" })
- <div class="col-md-10">
- @*Male
- @Html.RadioButtonFor(model => model.Gender, "Male")
- Female
- @Html.RadioButtonFor(model => model.Gender, "Female")*@
-
- </div>
- </div>
- <div class="form-group">
- @Html.LabelFor(model => model.SkillsID, htmlAttributes: new { @class = "control-label col-md-2" })
- <div 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" })
- </div>
- </div>
-
- <div class="form-group">
- @Html.LabelFor(model => model.MobileNumber, htmlAttributes: new { @class = "control-label col-md-2" })
- <div class="col-md-10">
- @Html.EditorFor(model => model.MobileNumber, new { htmlAttributes = new { @class = "form-control" } })
- @Html.ValidationMessageFor(model => model.MobileNumber, "", new { @class = "text-danger" })
- </div>
- </div>
-
- <div class="form-group">
- @Html.LabelFor(model => model.DesignationID, htmlAttributes: new { @class = "control-label col-md-2" })
- <div class="col-md-10">
- @Html.EditorFor(model => model.DesignationID, new { htmlAttributes = new { @class = "form-control" } })
- @Html.ValidationMessageFor(model => model.DesignationID, "", new { @class = "text-danger" })
- </div>
- </div>
-
- <div class="form-group">
- @Html.LabelFor(model => model.Salary, htmlAttributes: new { @class = "control-label col-md-2" })
- <div class="col-md-10">
- @Html.EditorFor(model => model.Salary, new { htmlAttributes = new { @class = "form-control" } })
- @Html.ValidationMessageFor(model => model.Salary, "", new { @class = "text-danger" })
- </div>
- </div>
-
- <div class="form-group">
- @Html.LabelFor(model => model.DepartmentID, htmlAttributes: new { @class = "control-label col-md-2" })
- <div 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" })
- </div>
- </div>
-
- <div class="form-group">
- @Html.LabelFor(model => model.CountryID, htmlAttributes: new { @class = "control-label col-md-2" })
- <div 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" })
- </div>
- </div>
-
- <div class="form-group">
- @Html.LabelFor(model => model.CityID, htmlAttributes: new { @class = "control-label col-md-2" })
- <div 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" })
- </div>
- </div>
-
- <div class="form-group">
- <div class="col-md-offset-2 col-md-10">
- <input type="submit" value="Create" class="btn btn-default" />
- </div>
- </div>
- </div>
- }
-
- <div>
- @Html.ActionLink("Back to List", "List")
- </div>
-
- <script src="~/Scripts/jquery-1.10.2.min.js"></script>
- <script src="~/Scripts/jquery.validate.min.js"></script>
- <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
- <script>
- $(document).ready(function () {
- $("#CountryID").change(function () {
-
- $.ajax({
- type: "POST",
- url: '@Url.Action("GetCity", "Employee")',
-
- data: { CountryID: $(this).val() },
- dataType: "json",
- success: function (response) {
- let options = '';
- options += '<option value=0>-Select-</option>';
- $.each(response, function (i, item) {
- options += '<option value='+item.CityID+'>'+item.CityName+'</option>';
-
- })
- ;
- $('#CityID').html(options);
- },
- error: function (error) {
- debugger
- },
- });
- });
- });
- </script>
-
- List View
- @model IEnumerable<Learning1.Models.EmployeeDTO>
- @using Learning1.Models;
- @{
- ViewBag.Title = "List";
- LeaningOneEntities dbEntity = new LeaningOneEntities();
- }
-
- <h2>List</h2>
-
- <p>
- @Html.ActionLink("Create New", "AddNew","Employee")
- </p>
- <table class="table">
-
- <tr>
- <th>
- @Html.DisplayNameFor(model => model.EmpName)
- </th>
- <th>
- @Html.DisplayNameFor(model => model.Email)
- </th>
- <th>
- @Html.DisplayNameFor(model => model.MobileNumber)
- </th>
- <th>
- @Html.DisplayName("Designation")
- </th>
- <th>
- @Html.DisplayNameFor(model => model.Salary)
- </th>
- <th>
- @Html.DisplayName("Department")
- </th>
- <th>
- @Html.DisplayName("Country")
- </th>
- <th>
- @Html.DisplayName("City")
- </th>
-
- <th>
- @Html.DisplayName("Skills")
- </th>
- <th></th>
- </tr>
-
- @foreach (var item in Model) {
- var skill = "";
- <tr>
- <td>
- @Html.DisplayFor(modelItem => item.EmpName)
- </td>
- <td>
- @Html.DisplayFor(modelItem => item.Email)
- </td>
- <td>
- @Html.DisplayFor(modelItem => item.MobileNumber)
- </td>
- <td>
- @Html.DisplayFor(modelItem => item.DesignationName)
- </td>
- <td>
- @Html.DisplayFor(modelItem => item.Salary)
- </td>
- <td>
- @Html.DisplayFor(modelItem => item.DepartmentName)
- </td>
- <td>
- @Html.DisplayFor(modelItem => item.CountryName)
- </td>
- <td>
- @Html.DisplayFor(modelItem => item.CityName)
- </td>
-
- @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 + ",";
-
- }
- <td>
- @Html.DisplayFor(modelItem => skill)
- </td>
- <td>
- @Html.ActionLink("Edit", "EditEmployee", new { EmpId = item.EmpID }) |
- @Html.ActionLink("Details", "Details", new { id=item.EmpID }) |
- @Html.ActionLink("Delete", "DeleteEmploye", new { EmpId = item.EmpID })
- </td>
- </tr>
- }
-
- </table>
-
-
- 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
- {
-
- 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
-
- 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()
- }).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<Tbl_EmpSkills> empSkills = new List<Tbl_EmpSkills>();
- 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<Tbl_Skills> objskill = new List<Tbl_Skills>();
- 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<Tbl_EmpSkills> empList = new List<Tbl_EmpSkills>();
- 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<SkillsetDTO> 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
- 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<Tbl_Country> CountryList { get; set; }
- public List<Tbl_City> CityList { get; set; }
- public List<Tbl_Department> DepartmentList { get; set; }
-
- public List<Tbl_Skills> SkillList { get; set; }
- public List<Tbl_EmpSkills> empSkillSet { get; set; }
-
-
-
- public Tbl_employee()
- {
- CountryList = new List<Tbl_Country>();
- CityList = new List<Tbl_City>();
- SkillList = new List<Tbl_Skills>();
- empSkillSet = new List<Tbl_EmpSkills>();
-
- }
-
- }
-
- 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
- {
-
- }
- }