سؤال

I'm very first experience in ASP.NET MVC and after I read through a few articles I decided I try my new project with MVC.

And I used ADO.net entity data model and I created Create/ Delete/ Details/ Edit/ Index. It works fine. So I plan to improve in user interface because some of the fields are comes from another databases such as HR Database, etc... for employee information.

for e.g : to choose Employee's Name on my form, I have to use DropDownList and that data comes from another database, HR as I mentioned in above. I have no idea how to access difference database in one model and here I asked How to solve multiple databases in one edmx for ASP.net MVC?

However I tried to create one more model for the other database and try to join with 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());
    }

But I got this error

The model item passed into the dictionary is of type 'System.Collections.Generic.List1[<>f__AnonymousType29[System.Int32,System.Int32,System.String,System.String,System.DateTime,System.DateTime,System.String,System.Boolean,Access_Request_System.Models.Genre]]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[Access_Request_System.Models.RequestForm]'.

Really no idea for this case... Please!

هل كانت مفيدة؟

المحلول

Your view file requires IEnumerable<RequestForm>, but the type you're passing does not match the required type (it's anonymous).

Try the following changes.

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);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top