0

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!

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

2 Comments

so you mean that I need to use ViewModel, right? If It is so, How do I use dropdownlist in EditTemplate for e.g : want to use ddl for PrjDeptMgr and bind form another db and thanks for your answer...
The value you're passing to the view using View() is your view model. You're view model type as specified in the view file is OK. It's the value you're passing to it (List<RequestForm>) that was incorrect. The drop down list will need to be populated somehow (that's up to you). I'm not aware of the edit template supporting dropdown lists AFAIK.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.