What is the problem with my code it shows error as (An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code)[Answered]RSS
public ActionResult GetData()
{
SchoolEntities2 entities = new SchoolEntities2();
var query = from Student in entities.Students
from Student_Courses in Student.Student_Courses
select new
{
StudentName = Student.Name,
CourseName = Student_Courses.Course,
EnrolledDate = Student_Courses.DateOfEnrolment
};
var res = query.ToList();
return View(res);
}
This is my model
public class StudentInfo
{
public string Name { get; set; }
public Nullable<System.DateTime> DateOfEnrolment { get; set; }
public virtual Course Course { get; set; }
}
System.InvalidOperationException: The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[<>f__AnonymousType2`3[System.String,WebApplication1.Models.Course,System.Nullable`1[System.DateTime]]]', but this dictionary requires
a model item of type 'System.Collections.Generic.List`1[WebApplication1.Models.StudentInfo]
I have search on internet but couldn't understand help please
Member
11 Points
24 Posts
What is the problem with my code it shows error as (An unhandled exception occurred during the ex...
Apr 17, 2020 03:24 PM|jameslovemicrosoft|LINK
The following is the code I have done.
This is my model
This is my view:
the error is
System.InvalidOperationException: The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[<>f__AnonymousType2`3[System.String,WebApplication1.Models.Course,System.Nullable`1[System.DateTime]]]', but this dictionary requires a model item of type 'System.Collections.Generic.List`1[WebApplication1.Models.StudentInfo]
I have search on internet but couldn't understand help please
All-Star
58194 Points
15661 Posts
Re: What is the problem with my code it shows error as (An unhandled exception occurred during th...
Apr 17, 2020 03:29 PM|bruce (sqlwork.com)|LINK
C# is strongly typed, your query returns an list of an anonymous object:
new { StudentName = Student.Name, CourseName = Student_Courses.Course, EnrolledDate = Student_Courses.DateOfEnrolment }
not a List<StudentInfo>