I have a task to make a program on ASP.NET MVC C #: "We have a company, the company has employees. Each employee is divided into 2 types (designer and programmer). They also have their own skill: (if a designer, then Photoshop, graphics ..., programmer: PHP, Java, C, C ++ ...). If we created a designer, we won’t be able to add a programmer’s skill to him and vice versa.

public class Employee { public int EmployeeID { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string EmployeeType { get; set; } } 

Also a class with skills.

 public class Skill { public int SkillID { get; set; } public string SkillName { get; set; } public string SkillType { get; set; } } 

And the general class.

 public class EmployeeSkill { public int EmployeeSkillID { get; set; } public int EmployeeID { get; set; } public int SkillID { get; set; } } 

Entity framework.

 public class EFDbContext : DbContext { public DbSet<Employee> Employees { get; set; } public DbSet<Skill> Skills { get; set; } public DbSet<EmployeeSkill> SkillOfEmployee { get; set; } } 

Created a general model for all this.

 public class EmployeesListViewModel { public Employee Employee { get; set; } public IEnumerable<Skill> Skills { get; set; } public IEnumerable<EmployeeSkill> EmployeeSkills { get; set; } } 

Here is the controller.

 public class HomeController : Controller { private EFDbContext db = new EFDbContext(); public ActionResult Index() { return View(db.Employees); } public ActionResult Details(int id = 0) { EmployeesListViewModel model = new EmployeesListViewModel(); try { model.Employee = db.Employees.Find(id); //тут проблема model.EmployeeSkills = db.SkillOfEmployee.Where(m => m.EmployeeID == id); model.Skills = db.Skills.Where(m => m.SkillType == db.Employees.Find(id).EmployeeType); } catch (Exception e) { // ViewBag.Message = e.Message; return RedirectToAction("Index"); } return View(model); } public ActionResult Create() { return View(); } [HttpPost] public ActionResult Create(Employee employee) { if (ModelState.IsValid) { db.Employees.Add(employee); db.SaveChanges(); return RedirectToAction("Index"); } return View(employee); } public ActionResult Edit(int id = 0) { Employee employee = db.Employees.Find(id); if (employee == null) { return HttpNotFound(); } return View(employee); } [HttpPost] public ActionResult Edit(Employee employee) { if (ModelState.IsValid) { db.Entry(employee).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } return View(employee); } public ActionResult Delete(int id = 0) { Employee employee = db.Employees.Find(id); if (employee == null) { return HttpNotFound(); } return View(employee); } [HttpPost, ActionName("Delete")] public ActionResult DeleteConfirmed(int id) { Employee employee = db.Employees.Find(id); db.Employees.Remove(employee); db.SaveChanges(); return RedirectToAction("Index"); } protected override void Dispose(bool disposing) { db.Dispose(); base.Dispose(disposing); } } 

Help me to correctly form the wiki and solve the problem on the stage, when I search in the user base I get an error that "invalid cast" why (Details method in the controller)?

  • And on Index at you falls nothing? Maybe you just have to redirect with Details on Index and it actually falls? Try throwing the db.Employees.ToList() - MihailPw
  • Cannot call the "DXWebApplication1.Models.Employee Find (System.Object [])" method declared for the type "System.Data.Entity.DbSet 1[DXWebApplication1.Models.Employee]", с экземпляром типа "System.Data.Objects.ObjectQuery 1 [DXWebApplication1.Models.Employee] " - Rostik Boretsky
  • Did not help with ToList (); - Rostik Boretsky
  • Maybe the problem is that Find () is looking for an array, and I transfer it to one model? - Rostik Boretsky
  • In theory, everything is correct here msdn.microsoft.com/en-us/library/gg696418(v=vs.113).aspx - MihailPw

0