Click here to Skip to main content
15,891,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an Action Method in MVC application that retrieves the list of Students from a database table StudentsInfo using LINQ query. When i retrieve all the columns from the table then the Query works but when i try to retrieve some specific columns from the table then i get the following error message:

Quote:
The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[<>f__AnonymousType6`1[System.String]]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[MVCApplication.StudentsInfo]'.


What I have tried:

Following is the action Method

public ActionResult GetAllStudents()
        {
            //Retrieve data from StudentsInfo table
           var result = from d in db.StudentsInfo
                        select new
                        {
                            d.StdRegNumber,
                           d.StdName,
                           d.StdDoBirth
                        };

           
            return View(result.ToList());
        }
Posted
Updated 14-Aug-18 4:35am

When you use "new { ... }" syntax you're creating what's known as an anonymous type, look on at like a temporary type or a type created on-the-fly. Anonymous types can only be used inside the function they are created in, so you can't pass that result to a View.

If you want a reduced number of columns then create a new concrete class called something like StudentViewModel that has the three fields you want, then change the code to something like

select new StudentViewModel
{
    StdRegNumber = d.StdRegNumber,
    StdName = d.StdName,
    StdDoBirth = d.StdDoBirth
};


You'll also need to change the view to use a model of IEnumerable<<StudentViewModel>

Or you could just pass the original studentsinfo list to the view and simply ignore the fields you're not interested in.
 
Share this answer
 
Comments
Vincent Maverick Durano 14-Aug-18 10:37am    
5ed
As stated by F-ES Sitecore , when you use the new keyword, your return type will be converted to anonymous type. You are getting an error because your View expects a strongly-type model IEnumerable<StudentsInfo> but you were returning an anonymous type.

In order to fix that, you could return a dynamic type or ExpandoObject as what is demonstrated in this article: Binding views with Anonymous type collection in ASP.NET MVC - DotNetFunda.com[^]

Keep in mind that when going to an anonymous or dynamic type, you will lose all the good stuff that a strong-type has. (e.g compile-time checking and intellisense for your View). So think about it. The Controller could end up passing any type to the View while at runtime. We can't even analyze the code and guess, because action filters could change the object passed to the View for all we know.

My recommendation is use strongly-typed View and return a concrete model which houses only the fields that you want in the View just like what is already suggested by F-ES Sitecore.
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900