ahmed salah

ahmed salah

  • NA
  • 530
  • 141.8k

How to pass data from index view to edit view

Aug 25 2016 3:04 PM
when click edit link in index page
it give me error
The model item passed into the dictionary is of type 'System.Data.Entity.DynamicProxies.Employee_2EF71CC17A29BA91B02BC5CDB0EE5AF82D363EEF7E174A21C9546772913AA929', but this dictionary requires a model item of type 'WebCourse.Models.Customemployee'.
I have custom model
  1. namespace WebCourse.Models Customemployee  
  2. {  
  3. public class Customemployee  
  4. {  
  5. public string Name { getset; }  
  6. public int Salary { getset; }  
  7. public string Email { getset; }  
  8. public int DistrictId { getset; }  
  9.   
  10. public List<EmployeeCourse> Courses { getset; }  
  11. public List<EmployeeLangage> Langs { getset; }  
  12. }  
  13.   
  14. }  
and my controller empcourse
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. using WebCourse.Models;  
  7. using System.Data.Entity;  
  8. namespace WebCourse.Controllers  
  9. {  
  10. public class empcourseController : Controller  
  11. {  
  12. mycourseEntities db = new mycourseEntities();  
  13. // GET: empcourse  
  14. public ActionResult Index()  
  15. {  
  16. var query = db.Employees.ToList().Select(p => new EmpInfo  
  17. {  
  18. Id = p.Id,  
  19. Name = p.Name,  
  20. Salary = Convert.ToInt32( p.Salary),  
  21. Email = p.Email,  
  22. DistrictName = p.Destrict.DistrictName,  
  23. CityName = p.Destrict.City.CityName,  
  24. CountryName = p.Destrict.City.Country.CountryName,  
  25. CourseNames = p.EmployeeCourses.Select(t => t.Course.CourseName).ToList(),  
  26. LanguageName = p.EmployeeLangages.Select(t => t.Language.LnaguageName).ToList(),  
  27. levelName = p.EmployeeLangages.Select(t => t.Level.LevelName).ToList(),  
  28. CourseName = string.Join(",", p.EmployeeCourses.Select(t => t.Course.CourseName).ToList())  
  29. });  
  30.   
  31. return View(query);  
  32. }  
  33. public ActionResult Create()  
  34. {  
  35. ViewBag.CountryId = new SelectList(db.Countries, "Id""CountryName");  
  36. ViewBag.LanaguageId = new SelectList(db.Languages.ToList(), "Id""LnaguageName");  
  37. ViewBag.LevelId = new SelectList(db.Levels.ToList(), "Id""LevelName");  
  38. ViewBag.CourseId = new SelectList(db.Courses.ToList(), "Id""CourseName");  
  39. return View();  
  40. }  
  41. public ActionResult Edit(int id)  
  42. {  
  43. //how to pass data from index view to edit view  
  44. Employee old = db.Employees.Find(id);  
  45. return View(old);  
  46. }  
  47. [HttpPost]  
  48. public ActionResult Create(Customemployee cemp)  
  49. {  
  50.   
  51. using (mycourseEntities db = new mycourseEntities())  
  52. {  
  53. Employee E = new Employee { Name = cemp.Name, Salary = cemp.Salary, Email = cemp.Email, DistrictId = cemp.DistrictId };  
  54. foreach (var i in cemp.Courses)  
  55. {  
  56.   
  57. E.EmployeeCourses.Add(i);  
  58. db.SaveChanges();  
  59. }  
  60. foreach (var i in cemp.Langs)  
  61. {  
  62.   
  63. E.EmployeeLangages.Add(i);  
  64. db.SaveChanges();  
  65. }  
  66. db.Employees.Add(E);  
  67. db.SaveChanges();  
  68. }  
  69. return View();  
  70. }  
  71. public JsonResult getcitybyid(int id)  
  72. {  
  73. db.Configuration.ProxyCreationEnabled = false;  
  74. return Json(db.Cities.Where(a => a.CountryId == id), JsonRequestBehavior.AllowGet);  
  75. }  
  76. public JsonResult getdistrictbyid(int id)  
  77. {  
  78. db.Configuration.ProxyCreationEnabled = false;  
  79. return Json(db.Destricts.Where(a => a.CityId == id), JsonRequestBehavior.AllowGet);  
  80. }  
  81. }  
  82. }  

Answers (2)