Remark :
DBModel db = new DBModel();
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Web; namespace EmployeeMaintenance.Models { public class Department { [Key] public int DepartmentId { get; set; } [MaxLength(50)] public string DepartmentName { get; set; } //public virtual ICollection<Employee> Employees { get; set; } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Web; namespace EmployeeMaintenance.Models { public class Employee { //Table properties [Key] public int EmployeeId { get; set; } public int DepartmentId { get; set; } [MaxLength(50)] public string FirstName { get; set; } [MaxLength(50)] public string LastName { get; set; } [MaxLength(50)] public string Address1 { get; set; } [MaxLength(50)] public string Address2 { get; set; } [MaxLength(50)] public string City { get; set; } [MaxLength(50)] public string State { get; set; } [MaxLength(50)] public string Country { get; set; } [MaxLength(10)] public string PostalCode { get; set; } [MaxLength(100)] public string Email { get; set; } public DateTime? DOB { get; set; } [MaxLength(10)] public string Gender { get; set; } public bool IsPermanent { get; set; } public decimal Salary { get; set; } public virtual Department Department { get; set; } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
using System; using System.Collections.Generic; using System.Data.Entity; using System.Data.Entity.Infrastructure; using System.Linq; using System.Web; namespace EmployeeMaintenance.Models.DL { public class DBModel : DbContext { public DBModel() : base("name=DBModel") { } public DbSet<Department> Departments { get; set; } public DbSet<Employee> Employees { get; set; } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
using EmployeeMaintenance.Models; using EmployeeMaintenance.Models.DL; using System; using System.Collections.Generic; using System.Data.Entity; using System.Linq; using System.Web; namespace EmployeeMaintenance.App_Start { public class DBInitializer { public static void CreateAndInitializeDatabase() { Database.SetInitializer(new DropCreateDatabaseIfModelChanges<DBModel>()); DBModel db = new DBModel(); if (db.Departments.Count() <= 0) { Department department = new Department(); department.DepartmentName = "IT"; db.Departments.Add(department); department = new Department(); department.DepartmentName = "CSE"; db.Departments.Add(department); } if (db.Employees.Count() <= 0) { Employee emp = new Employee(); emp.FirstName = "Tom"; emp.LastName = "Hanks"; emp.Address1 = "1St Street"; emp.Address2 = "Columbia Ave"; emp.City = "Los Angeles"; emp.State = "CA"; emp.Country = "US"; emp.PostalCode = "123456"; emp.DepartmentId = 1; db.Employees.Add(emp); emp = new Employee(); emp.FirstName = "Bruce"; emp.LastName = "Willis"; emp.Address1 = "2nd Street"; emp.Address2 = "Farmers Ave"; emp.City = "Seattle"; emp.State = "WA"; emp.Country = "US"; emp.PostalCode = "756446"; emp.DepartmentId = 2; db.Employees.Add(emp); emp = new Employee(); emp.FirstName = "Johnny"; emp.LastName = "Depp"; emp.Address1 = "5Th Street"; emp.Address2 = "Shuttle Ave"; emp.City = "San Antonio"; emp.State = "TX"; emp.Country = "US"; emp.PostalCode = "123456"; emp.DepartmentId = 2; db.Employees.Add(emp); emp = new Employee(); emp.FirstName = "Christian"; emp.LastName = "Bale"; emp.Address1 = "8th Street"; emp.Address2 = "SpaceX Ave"; emp.City = "Chicago"; emp.State = "IL"; emp.Country = "US"; emp.PostalCode = "123456"; emp.DepartmentId = 1; db.Employees.Add(emp); emp = new Employee(); emp.FirstName = "Heath"; emp.LastName = "Ledger"; emp.Address1 = "7th Street"; emp.Address2 = "Discovery Ave"; emp.City = "Los Angeles"; emp.State = "CA"; emp.Country = "US"; emp.PostalCode = "123456"; emp.DepartmentId = 1; db.Employees.Add(emp); } db.SaveChanges(); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
using EmployeeMaintenance.App_Start; using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Web.Optimization; using System.Web.Routing; namespace EmployeeMaintenance { public class MvcApplication : System.Web.HttpApplication { protected void Application_Start() { AreaRegistration.RegisterAllAreas(); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); DBInitializer.CreateAndInitializeDatabase(); } } } |
사용
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
using EmployeeMaintenance.Models; using EmployeeMaintenance.Models.DL; using System; using System.Collections.Generic; using System.Data.SqlClient; using System.Linq; using System.Web; using System.Web.Mvc; namespace EmployeeMaintenance.Controllers { public class EmployeeController : Controller { DBModel db = new DBModel(); public ActionResult Index() { return View(); } public ActionResult details(int id) { return View(db.Employees.Where(e => e.EmployeeId == id).FirstOrDefault()); } public ActionResult EmployeeSummary() { IList<Employee> empSummary = db.Employees.ToList(); return View(empSummary); } public JsonResult detailasjson(int id) { return Json(db.Employees.Where(e => e.EmployeeId == id).FirstOrDefault(), JsonRequestBehavior.AllowGet); } [HttpPost] public ActionResult delete(int id) { Employee emp = db.Employees.Where(e => e.EmployeeId == id).FirstOrDefault(); if(emp != null) { db.Employees.Remove(emp); db.SaveChanges(); return Content("Deleted Successfully!"); } return Content("Error!"); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
@using EmployeeMaintenance.Models; @model IEnumerable<Employee> @{ ViewBag.Title = "Index"; Layout = "~/Views/Shared/_Layout.cshtml"; } @section css { } <h2>Employee List</h2> <div> @Html.ActionLink("Index", "Index", "Home") </div> <div> <table class="table table-bordered table-condensed"> <tr> <th>Name</th> <th>Department</th> <th>jQuery AJAX</th> </tr> @foreach (Employee emp in Model) { <tr id="tr@(emp.EmployeeId)"> <td> @Html.ActionLink(String.Format("{0} {1}", emp.FirstName, emp.LastName), "Details", new { id = emp.EmployeeId }) </td> <td> @emp.Department.DepartmentName </td> <td> <a class="details" id="@emp.EmployeeId" href="#">Details</a> | <a class="delete" id="@emp.EmployeeId" href="#">Delete</a> </td> </tr> } </table> </div> <div id="details"> </div> @section scripts { <script type="text/javascript"> $(document).ready(function () { $('a.details').click(function () { var id = $(this).attr("id"); $.ajax({ url: '/employee/detailasjson', type: "GET", dataType: "json", data: {id:id}, success: function (data) { content = "<h3>Employee Detail</h3>"; content += "<dl class=\"dl-horizontal\">"; content += "<dt>First Name</dt><dd>" + data.FirstName + "</dd>"; content += "<dt>Last Name</dt><dd>" + data.LastName + "</dd>"; content += "<dt>Address1</dt><dd>" + data.Address1 + "</dd>"; content += "<dt>Address2</dt><dd>" + data.Address2 + "</dd>"; content += "<dt>City</dt><dd>" + data.City + "</dd>"; content += "<dt>State</dt><dd>" + data.State + "</dd>"; content += "<dt>Country </dt><dd>" + data.Country + "</dd>"; content += "<dt>PostalCode</dt><dd>" + data.PostalCode + "</dd>"; content += "<dt>Email </dt><dd>" + data.Email + "</dd>"; content += "<dt>DOB </dt><dd>" + data.DOB + "</dd>"; content += "<dt>Gender </dt><dd>" + data.Gender + "</dd>"; content += "<dt>IsPermanent </dt><dd>" + data.IsPermanent + "</dd>"; content += "<dt>Salary </dt><dd>" + data.Salary + "</dd>"; content += " </dl>"; $('#details').html(content); }, error: function (xhr, status, error) { alert(xhr.responseText); alert(status); alert(error); } }); }); $('a.delete').click(function () { var id = $(this).attr("id"); $.ajax({ url: '/employee/delete', type: "POST", dataType: "text", data: { id: id }, success: function (data) { $('#tr' + id).html(data); }, error: function (xhr, status, error) { alert(xhr.responseText); alert(status); alert(error); } }); }); }); </script> } |