I need to check if content area is empty but I get error "Object referrence not set to an instance" , this is my page controller i've tried also currentPage.TabContentArea.IsEmpty
, still, same error. Content area is empty, this is first time Im trying to run it so I need to check is it empty before executing code inside if statement.
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 ?
}
}
The ContentArea property can be null, so you need to check currentPage.TabContentArea for null first.
if(currentPage.TabContentArea != null && currentPage.TabContentArea.FilteredItems.Any()) { ... }