Skip to content
Loading
Error in mvc view.
Controller Code:
  1. public IActionResult EducationalVideo()  
  2. {  
  3. EducationalVideoList vm = new EducationalVideoList();  
  4. List lst = new List();  
  5. try  
  6. {  
  7. var Video = _context.TblEducationalVideo.ToList();  
  8. if (Video != null)  
  9. {  
  10. foreach (var item in Video)  
  11. {  
  12. EducationalVideoViewModel lst_Data = new EducationalVideoViewModel();  
  13. lst_Data.Name = item.Name;  
  14. lst_Data.Video = item.VideoPath;  
  15. lst.Add(lst_Data);  
  16. }  
  17. vm.lst_EducationData = lst;  
  18. }  
  19. else  
  20. {  
  21. ViewBag.message = "No Record !";  
  22. }  
  23. }  
  24. catch(Exception ex)  
  25. {  
  26. ViewBag.message = "Internal Server Error";  
  27. var message = ex.InnerException.Message;  
  28. throw;  
  29. }  
  30. return View(vm);  
  31. }  
  • Varun Setia
    Hi,
     
    Since, the list is not defined it can throw null exception. You can by default define the constructor for this case.
     
    1. public class EducationalVideoList  
    2. {  
    3. public List lst_EducationData { get;set; }  
    4. public string Name { getset; }  
    5. public string Video { getset; }  
    6. public string Image { getset; }  
    7.   
    8. public EducationalVideoList(){  
    9. lst_EducationData = new List();  
    10. }  
    11.   
    12. }