如何将数据从索引视图传递到编辑视图

时间:2016-08-25 20:02:07

标签: asp.net-mvc-4 linq-to-sql linq-to-entities

单击索引页面中的编辑链接 它给我错误 传递到字典中的模型项的类型为&System; System.Data.Entity.DynamicProxies.Employee_2EF71CC17A29BA91B02BC5CDB0EE5AF82D363EEF7E174A21C9546772913AA929',但此字典需要类型为' WebCourse.Models.Customemployee'的模型项。 我有自定义模型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; }

    }

}

和我的控制者经纪人

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

错误显示在代码

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

            }

如何将显示数据的索引视图中的数据传递给编辑视图

1 个答案:

答案 0 :(得分:0)

从错误消息中,您的编辑视图显示为Customemployee类型。这意味着您应该将编辑操作方法中的Customemployee类对象传递给此视图。但是您当前的代码是发送Employee类型的对象。

因此,请更新您的编辑操作方法,以将正确的对象发送到视图。

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
}