본문 바로가기
기타

[ASP.NET Core/APOD] InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1+AsyncStateMachineBox`1[System.Collections.Generic.List`1[APOD.Models.APODModel],Microso..

by 항붕쿤 2023. 3. 16.

이 오류는 뷰에서 기대하는 모델의 형식과 실제로 전달된 모델의 형식이 일치하지 않을 때 발생한다.

나같은 경우에 뷰에서는 IEnumerable 형식의 모델을 예상했지만 실제로는 List형식의 데이터를 뷰에 전달하여 오류가 생겼다.

IEnumerable<T>는 데이터를 순환하면서 처리할 때 사용하는 데이터 형식이고, List<T>는 IEnumerable<T>를 상속받은 클래스로서 동적으로 크기가 조절될 수 있는 리스트 형태의 컬렉션이다.
(말이 조금 어렵다)

쉽게 말하면 IEnumerable는 읽기전용 리스트, List는 읽고 쓰기가 모두 가능한 리스트이다.

뷰가 예상한 모델형식
전달된 모델 형식

return View(_context.APODModel.ToListAsync());

=> return View(await _context.APODModel.ToListAsync()); 으로 수정하니 해결됐다.