Results 1 to 3 of 3

Thread: How to pass data from index view to edit view

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2016
    Posts
    37

    How to pass data from index view to edit view

    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_2EF71CC17A29BA91B02BC5CDB0EE5AF82D363EEF7E174A21C9546772 913AA929', but this dictionary requires a model item of type 'WebCourse.Models.Customemployee'.
    I have custom model
    namespace WebCourse.Models Customemployee
    {
    public class Customemployee
    {
    public string Name { get; set; }
    public int Salary { get; set; }
    public string Email { get; set; }
    public int DistrictId { get; set; }

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

    }

    }
    and my controller empcourse

    using System;
    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);
    }
    }
    }

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: How to pass data from index view to edit view

    Firstly, please don't post unformatted code because, as you can see, it's quite hard to read. The editor provides two buttons for formatting code.
    csharp Code:
    1. namespace WebCourse.Models Customemployee
    2. {
    3.     public class Customemployee
    4.     {
    5.         public string Name { get; set; }
    6.         public int Salary { get; set; }
    7.         public string Email { get; set; }
    8.         public int DistrictId { get; set; }
    9.  
    10.        public List<EmployeeCourse> Courses { get; set; }
    11.        public List<EmployeeLangage> Langs { get; set; }
    12.        
    13.     }
    14.  
    15. }
    csharp Code:
    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.         }
    34.         public ActionResult Create()
    35.         {
    36.             ViewBag.CountryId = new SelectList(db.Countries, "Id", "CountryName");
    37.             ViewBag.LanaguageId = new SelectList(db.Languages.ToList(), "Id", "LnaguageName");
    38.             ViewBag.LevelId = new SelectList(db.Levels.ToList(), "Id", "LevelName");
    39.             ViewBag.CourseId = new SelectList(db.Courses.ToList(), "Id", "CourseName");
    40.             return View();
    41.         }
    42.         public ActionResult Edit(int id)
    43.         {
    44. //how to pass data from index view to edit view
    45.             Employee old = db.Employees.Find(id);
    46.             return View(old);
    47.            
    48.         }
    49.       [HttpPost]
    50.         public ActionResult Create(Customemployee cemp)
    51.         {
    52.  
    53.            
    54.             using (mycourseEntities db = new mycourseEntities())
    55.             {
    56.                 Employee E = new Employee { Name = cemp.Name, Salary = cemp.Salary, Email = cemp.Email, DistrictId = cemp.DistrictId };
    57.                 foreach (var i in cemp.Courses)
    58.                 {
    59.  
    60.                     E.EmployeeCourses.Add(i);
    61.                     db.SaveChanges();
    62.                 }
    63.                 foreach (var i in cemp.Langs)
    64.                 {
    65.  
    66.                     E.EmployeeLangages.Add(i);
    67.                     db.SaveChanges();
    68.                 }
    69.                 db.Employees.Add(E);
    70.                 db.SaveChanges();
    71.             }
    72.             return View();
    73.         }
    74.         public JsonResult getcitybyid(int id)
    75.         {
    76.             db.Configuration.ProxyCreationEnabled = false;
    77.             return Json(db.Cities.Where(a => a.CountryId == id), JsonRequestBehavior.AllowGet);
    78.         }
    79.         public JsonResult getdistrictbyid(int id)
    80.         {
    81.             db.Configuration.ProxyCreationEnabled = false;
    82.             return Json(db.Destricts.Where(a => a.CityId == id), JsonRequestBehavior.AllowGet);
    83.         }
    84.     }
    85. }

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: How to pass data from index view to edit view

    As for the question, you invoke an action from the Index view and pass the required information via the URL or post data and then the action displays the Edit view and passes the appropriate model. Usually it's a case of an Index view providing a list of items and the link or button for each item will navigate to a URL like "/mycontroller/edit/N" where N is the ID of the record. The Edit action then retrieves the record with that ID and passes and appropriate model to the Edit view. There would be a very large number of examples about of doing that.

    With regards to your specific error message, as I said, your action has to pass an appropriate model to the view. If your view requires a WebCourse.Models.Customemployee object then that's what you have to pass it. You are apparently passing it an EF entity. That won't magically become some other type. It's up to you to map the data from that entity type to your specific view model.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width