2016-06-22 3 views
0

내용 영역이 비어 있는지 확인해야하지만 "객체 참조가 인스턴스로 설정되지 않았습니다"라는 오류가 발생합니다.이 또한 시도한 적이있는 페이지 컨트롤러입니다. currentPage.TabContentArea.IsEmpty, 여전히 같은 오류입니다. 내용 영역이 비어 있습니다. 처음 실행하면 Im을 실행하려고하므로 if 문 안에 코드를 실행하기 전에 비어 있는지 확인해야합니다. 먼저 널 위해 currentPage.TabContentArea를 확인해야하므로Episerver에서 내용 영역이 비어 있는지 확인하십시오.

 public class StandardPageController : PageController<StandardPage> 
    { 
     // GET: StandardPage 
     public ActionResult Index(StandardPage currentPage) 
     { 

      // this collection should be used in foreach loops 
      var tabItems = new List<TabViewModel>(); 


//this is where I get error 
      if(currentPage.TabContentArea.FilteredItems.Any()) 

{ 
      var contentAreaItems = currentPage.TabContentArea.FilteredItems.ToList(); 
      var contentLoader = ServiceLocator.Current.GetInstance<IContentLoader>(); 

      foreach (var contentAreaItem in contentAreaItems) 
      { 
       // get an instance of Tab Block 
       // If you didn't set any restrictions, ContentArea can contain anything. 
       // We need to check if blockData is of type PageTab 
       var blockData = contentLoader.Get<PageTab>(contentAreaItem.ContentLink); 
       if (blockData == null) continue; 

       tabItems.Add(new TabViewModel 
       { 

        Id = Guid.NewGuid(), 
        Title = blockData.TabTitle, 
        Text = blockData.TabContent 
       }); 
      } 
      ViewBag.items = tabItems; 
      } 
      return View(); // Should I return tabitems here ? 
     } 
    } 

답변

3

ContentArea 속성은 null이 될 수 있습니다.

if(currentPage.TabContentArea != null && currentPage.TabContentArea.FilteredItems.Any()) { ... } 
+0

올바른 해결책이었습니다. 감사합니다. :) – perkes456