TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
ahmed salah
NA
530
148.4k
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
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);
}
}
}
Reply
Answers (
2
)
show data from more tables relation
Linq to Sql query to search for Accent sensitive name