Witam. Nie działa mi wyświetlanie informacji o studencie. Dostaje błąd: The model item passed into the dictionary is of type 'System.Data.Entity.Infrastructure.DbQuery`1[WebApplication1.Models.Student]', but this dictionary requires a model item of type 'WebApplication1.Models.Student'.
Jak rozumiem to w akcji zwracam coś innego niż oczekuje widok.
Moja akcja:
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
var student = db.Students.Include("Courses").Where(p => p.ID == id);
if (student == null)
{
return HttpNotFound();
}
return View(student);
}
Tabele z relacją wiele do wielu
public class Course
{
public Course()
{
Students = new HashSet<Student>();
}
public int CourseID { get; set; }
public string Title { get; set; }
public int Credits { get; set; }
public Grade? Grade { get; set; }
public virtual ICollection<Student> Students { get; set; }
}
public class Student
{
public Student()
{
Courses = new HashSet<Course>();
}
public int ID { get; set; }
public string LastName { get; set; }
public string FirstMidName { get; set; }
public DateTime EnrollmentDate { get; set; }
public ICollection<Course> Courses { get; set; }
}
Chodzi o to że wyświetlam dane studenta wraz z jego wszystkimi kursami.
Widok mam silnie typowany więc oczekuje Model.Student
Jak to ładnie zrobić żeby działało?
Jedyne rozwiązanie które mi zadziałało to w widoku dałem @model List<[WebApplication1.Models.Student>
A w zapytaniu na końcu dołożyłem .ToList();
Czyli zwracam listę z jednym studentem i w widoku muszę robić np model[0].LastName
Wcześniej miałem coś takiego Student student=db.Student.find(id);
I tu otrzymałem dane studenta i ładnie pokazywało ale niestety nie wyświetlało mi jego kursów.
I pytanie poza konkursem. W widoku przy wyrażeniach lambda nie mogę zobaczyć intellisense. Tzn pojawia się i znika szybko. Jest jakiś sposób na to?