0

我是 ASP.NET MVC 的第一次体验,在阅读了几篇文章后,我决定尝试使用 MVC 进行我的新项目。

我使用了 ADO.net 实体数据模型并创建了 Create/Delete/Details/Edit/Index。它工作正常。所以我计划改进用户界面,因为一些字段来自另一个数据库,如 HR 数据库等......用于员工信息。

例如:要在我的表单上选择员工姓名,我必须使用 DropDownList 并且该数据来自另一个数据库,如我上面提到的 HR。我不知道如何在一个模型中访问差异数据库,在这里我问如何在一个 edmx 中为 ASP.net MVC 解决多个数据库?

但是,我尝试为另一个数据库再创建一个模型并尝试加入 Linq。

//
    // GET: /ARS/

    public ActionResult Index()
    {
        var employee = new List<EMPLOYEE>(chr.EMPLOYEEs);
        var reqform = new List<RequestForm>(ars.RequestForms.Include(r => r.Genre));

        var requestforms = from r in reqform
                           join e in employee on r.PrjDeptMgr equals e.EmployeeID
                           select new
                             {
                                 r.FormID,
                                 r.GenreID,
                                 r.JobNo,
                                 r.Description,
                                 r.StartDate,
                                 r.EndDate,
                                 r.PrjDeptMgr,
                                 r.IsAgreed,
                                 r.Genre
                             };

        //var requestforms = ars.RequestForms.Include(r => r.Genre);
        return View(requestforms.ToList());
    }

但我得到了这个错误

传入字典的模型项的类型为“System.Collections.Generic.List 1[<>f__AnonymousType29[System.Int32,System.Int32,System.String,System.String,System.DateTime,System.DateTime,System.String,System。 Boolean,Access_Request_System.Models.Genre]]',但此字典需要“System.Collections.Generic.IEnumerable`1[Access_Request_System.Models.RequestForm]”类型的模型项。

真的不知道这种情况......拜托!

4

1 回答 1

1

您的视图文件需要IEnumerable<RequestForm>,但您传递的类型与所需的类型不匹配(它是anonymous)。

尝试以下更改。

List<RequestForm> requestforms = (from r in reqform
                       join e in employee on r.PrjDeptMgr equals e.EmployeeID
                       select new RequestForm
                         {
                             FormID = r.FormID,
                             Genre = r.GenreID,
                             JobNo = r.JobNo,
                             Description = r.Description,
                             StartDate = r.StartDate,
                             EndDate = r.EndDate,
                             PrjDeptMgr = r.PrjDeptMgr,
                             IsAgreed = r.IsAgreed,
                             Genre = r.Genre
                         }).ToList();

return View(requestForms);
于 2013-05-27T02:17:00.413 回答