1 Replies - 626 Views - Last Post: 26 August 2016 - 03:38 AM

#1 ahmedba  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 140
  • Joined: 24-January 14

How to pass data from index view to edit view

Posted 25 August 2016 - 12:40 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
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;
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);
        }
    }
}

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


Is This A Good Question/Topic? 0
  • +

Replies To: How to pass data from index view to edit view

#2 SixOfEleven  Icon User is offline

  • Planeswalker
  • member icon

Reputation: 1055
  • View blog
  • Posts: 6,643
  • Joined: 18-October 08

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

Posted 26 August 2016 - 03:38 AM

The reason why it is not passing will be in your view for Index action. Can you please post that code.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1