-1

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 Customemployee

namespace WebCourse.Models Customemployee
{
    public class Customemployee
    {

//represent employee table in database

     public string Name { get; set; }
        public int Salary { get; set; }
        public string Email { get; set; }
        public int DistrictId { get; set; }
//represent employee course table in database
       public List<EmployeeCourse> Courses { get; set; }

//represent employee language table in database

       public List<EmployeeLangage> Langs { get; set; }

    }

}

and my controller empcourse

using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebCourse.Models;
using System.Data.Entity;
namespace WebCourse.Controllers
{
    public class empcourseController : Controller
    {
        mycourseEntities db = new mycourseEntities();
        // GET: empcourse
        public ActionResult Index()
        {
            var query = db.Employees.ToList().Select(p => new EmpInfo
            {
                Id = p.Id,
                Name = p.Name,
                Salary = Convert.ToInt32( p.Salary),
                Email = p.Email,
                DistrictName = p.Destrict.DistrictName,
                CityName = p.Destrict.City.CityName,
                CountryName = p.Destrict.City.Country.CountryName,
                CourseNames = p.EmployeeCourses.Select(t => t.Course.CourseName).ToList(),
                LanguageName = p.EmployeeLangages.Select(t => t.Language.LnaguageName).ToList(),
                levelName = p.EmployeeLangages.Select(t => t.Level.LevelName).ToList(),
                CourseName = string.Join(",", p.EmployeeCourses.Select(t => t.Course.CourseName).ToList())
            });

            return View(query);

        }
        public ActionResult Create()
        {
            ViewBag.CountryId = new SelectList(db.Countries, "Id", "CountryName");
            ViewBag.LanaguageId = new SelectList(db.Languages.ToList(), "Id", "LnaguageName");
            ViewBag.LevelId = new SelectList(db.Levels.ToList(), "Id", "LevelName");
            ViewBag.CourseId = new SelectList(db.Courses.ToList(), "Id", "CourseName");
            return View();
        }
        public ActionResult Edit(int id)
        {
//how to pass data from index view to edit view
            Employee old = db.Employees.Find(id);
            return View(old);

        }
      [HttpPost]
        public ActionResult Create(Customemployee cemp)
        {


            using (mycourseEntities db = new mycourseEntities())
            {
                Employee E = new Employee { Name = cemp.Name, Salary = cemp.Salary, Email = cemp.Email, DistrictId = cemp.DistrictId };
                foreach (var i in cemp.Courses)
                {

                    E.EmployeeCourses.Add(i);
                    db.SaveChanges();
                }
                foreach (var i in cemp.Langs)
                {

                    E.EmployeeLangages.Add(i);
                    db.SaveChanges();
                }
                db.Employees.Add(E);
                db.SaveChanges();
            }
            return View();
        }
        public JsonResult getcitybyid(int id)
        {
            db.Configuration.ProxyCreationEnabled = false;
            return Json(db.Cities.Where(a => a.CountryId == id), JsonRequestBehavior.AllowGet);
        }
        public JsonResult getdistrictbyid(int id)
        {
            db.Configuration.ProxyCreationEnabled = false;
            return Json(db.Destricts.Where(a => a.CityId == id), JsonRequestBehavior.AllowGet);
        }
    }
}

the error show here in code

public ActionResult Edit(int id)
            {
    //how to pass data from index view to edit view
                Employee old = db.Employees.Find(id);
                return View(old);

            }

How to pass data from index view that show data to edit view

1 Answers1

0

From the error message, it looks like your Edit view is strontly typed to Customemployee type. That means you should be passing an object of Customemployee class from your Edit action method to this view. But your current code is sending an object of Employee type.

So update your Edit action method to send the correct object to the view.

public ActionResult Edit(int id)
{
   Employee old = db.Employees.Find(id);
   if(old!=null)
   {
     var vm = new CustomEmployee();
     vm.Name = old.Name;
     vm.Email = old.Email;
     // Assign other property values as needed.
     return View(vm);
   }  
   return Content("No employees found for the Id passed"); 
   // to do : Ideally return a "NotFound" view
}
Shyju
  • 214,206
  • 104
  • 411
  • 497
  • OK it working but i have problem issue when i get drop down list value in edit can you tell me how to get selected value of course and districtid in edit action result and view – user3223372 Aug 25 '16 at 20:22
  • Depends on how you are rendering the dropdown. Without seeing your view code, I cannot tell you. – Shyju Aug 25 '16 at 20:25
  • Country:@Html.DropDownList("CountryId")
    City:
    District:
    Courses:@Html.DropDownList("CourseId")
    i create view as above and how controller work it will show in create method above in controller and getcitybyid to make cascade between country and city and getdistrictid functions to make cascade between city and district
    – user3223372 Aug 25 '16 at 20:29
  • You should use `DropDownlListFor` helper method to make things easier. See a sample here http://stackoverflow.com/questions/36379535/how-to-properly-send-selectedvalue-of-dropdownlist-from-view-to-controller-view/36379772#36379772 – Shyju Aug 25 '16 at 20:31
  • this meaning i will change in Customemployee or what – user3223372 Aug 25 '16 at 20:47
  • I see you have a DistictId property ni your view model. For the dropdown option, you can add a new property of type List(Ex : `public List Districts { set;get;}`) and populate that in your Edit action with the districts in the db table. Then set vm.DistrictId to the value from your db(of Employee). The use `@Html.DropDownListFor(s=>s.DistrictId,Model.Districts)` – Shyju Aug 25 '16 at 21:29
  • I also recommend to **not ask a question which is unrelated to your original question in the comments**. You should post a new question with minimal relevant code. – Shyju Aug 25 '16 at 21:30
  • i make new model as Customemployee2 can you tell me what attribut i can put in it – user3223372 Aug 25 '16 at 21:35
  • i do as you tell me it give this exception The ViewData item that has the key 'DistrictId' is of type 'System.Int32' but must be of type 'IEnumerable'. what i do is create Customemployee2 model . in this model i add public int DistrictId { get; set; } public IEnumerable Districts { set; get; } and in view i add @Html.DropDownListFor(s => s.DistrictId, Model.Districts) and in edit i write vm.DistrictId = Convert.ToInt32(old.DistrictId); – user3223372 Aug 25 '16 at 21:55
  • As i suggested earlier. please do not ask questions in comments. Create a new a question with minimal but relevant code. – Shyju Aug 25 '16 at 21:55