this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[Monet.Models.AgentTransmission]'

asp.net-mvc asp.net-mvc-3 c# entity-framework linq

Question

I have two tables in my SQL database, one for storing the agent (AgentTransmission) and the other logs the transmission history (TransmissionHistory) of the agent (there will only be one successful, or reply status "S", record on the logging table per agent).

I need to grab the information from the AgentTransmission table according to a filter set based on the dates of the TransmissionHistory table. However, since the view I need to send this to is looking for @model IEnumerable<Monet.Models.AgentTransmission> I am getting the following error

The model item passed into the dictionary is of type 'System.Collections.Generic.List1[<>f__AnonymousTypeb'18[System.String,System.String,System.String,System.String,System.String,System.String,System.Int64,System.String,System.String,System.Nullable'1[System.DateTime],System.Boolean,System.String,System.String,System.String,System.String,System.String,System.String,System.Nullable'1[System.DateTime]]]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable'1[Monet.Models.AgentTransmission]'.

I'm pretty new to Linq/Entity Framework so if anyone can give me a hint as to what's going on that would be greatly appreciated. Thanks!

        var agents = (from a in db.AgentTransmission
                        join t in db.TransmissionHistory on
                            a.ID equals t.TranTableId
                        where a.RecordStatus.Equals("C") &&
                            a.WelcomeLetter &&
                            t.ReplyResult.Equals("S") &&
                            EntityFunctions.DiffDays(t.TransmittedOn, startDate) <= 0 &&
                            EntityFunctions.DiffDays(t.TransmittedOn, endDate) >= 0
                        select new
                        {
                            a.BankName,
                            a.DRMCompanyName,
                            a.Pendist,
                            a.FirstName,
                            a.LastName,
                            a.MiddleInitial,
                            a.ReferenceNumber,
                            a.AgencyId1,
                            a.AgencyIdType1,
                            a.EffectiveDate,
                            a.JIT,
                            a.Email,
                            a.LocationStreet1,
                            a.LocationStreet2,
                            a.LocationCity,
                            a.LocationState,
                            a.LocationZip,
                            a.CreatedDate
                        });


        return View(agents.ToList());
1
0
8/12/2013 8:22:45 PM

Accepted Answer

You are passing an anonymous object to your view (the new {} part of the select) whereas your view expects an object of type IEnumerable<AgentTransmission> (because that's what you used in your @model declaration). You should be coherent in the model you are passing to your view. Ideally design a view model and have your select statement return a new SomeViewModel and then make you view strongly typed to @model IEnumerable<SomeViewModel>.

2
8/12/2013 8:25:32 PM

Popular Answer

As the error is trying to tell you, you declared your view as taking a collection of AgentTransmission.

You can't pass it a collection of anonymous types.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow