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.