0

我是ASP.NET MVC的第一次經歷,在閱讀了幾篇文章後,我決定嘗試使用MVC創建新項目。ASP.NET MVC中的多個數據庫

而我使用ADO.net實體數據模型,並創建了創建/刪除/詳細信息/編輯/索引。它工作正常。 因此,我打算改進用戶界面,因爲某些字段來自其他數據庫,例如人力資源數據庫等等,用於員工信息。

例如:要在我的表單上選擇員工姓名,我必須使用DropDownList,並且該數據來自另一個數據庫HR,正如我在上面提到的。我不知道如何訪問一個模型中的差異數據庫,在這裏我問How to solve multiple databases in one edmx for 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__AnonymousType2 9 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]」。

真的不知道這種情況...請!

回答

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); 
+0

所以你的意思是我需要使用ViewModel,對不對? 如果是這樣的話,我該如何在EditTemplate 中使用下拉列表,例如:想要使用ddl作爲PrjDeptMgr並綁定另一個分區 並感謝您的回答...... – CMMaung

+0

您傳遞給視圖的值使用' View()'是你的視圖模型。您在視圖文件中指定的視圖模型類型是OK。這是你傳遞給它的價值('List '),這是不正確的。下拉列表需要以某種方式填充(這取決於你)。我不知道編輯模板支持下拉列表AFAIK。 –

+0

感謝您的回答:) – CMMaung