It is quite clear that Model.CurrentPage is null. What does your controller look like?
I have two controllers: PageControllerBase and StartPageController.
PageControllerBase:
using AlloyTraining.Business.ExtensionMethods;
using AlloyTraining.Models.Pages;
using AlloyTraining.Models.ViewModels;
using EPiServer;
using EPiServer.Core;
using EPiServer.Filters;
using EPiServer.Web.Mvc;
using System.Linq;
using System.Web.Mvc;
using System.Web.Security;
namespace AlloyTraining.Controllers
{
public class PageControllerBase<T> : PageController<T> where T : SitePageData
{
protected readonly IContentLoader loader;
public PageControllerBase(IContentLoader loader)
{
this.loader = loader;
}
public ActionResult Logout()
{
FormsAuthentication.SignOut();
return RedirectToAction("Index");
}
protected IPageViewModel<TPage> CreatePageViewModel<TPage>(
TPage currentPage) where TPage : SitePageData
{
var viewmodel = PageViewModel.Create(currentPage);
viewmodel.Startpage = loader.Get<StartPage>(ContentReference.StartPage);
viewmodel.MenuPages = FilterForVisitor.Filter(
loader.GetChildren<SitePageData>(ContentReference.StartPage))
.Cast<SitePageData>().Where(page => page.VisibleInMenu);
viewmodel.Section = currentPage.ContentLink.GetSection();
return viewmodel;
}
}
}
StartpageController:
using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using EPiServer.Core;
using EPiServer.DataAbstraction;
using EPiServer.DataAnnotations;
using EPiServer.SpecializedProperties;
namespace AlloyTraining.Models.Pages
{
[ContentType(DisplayName = "Start", GUID = "f41e57f2-67ba-46e0-a0f8-4855ea9208a4", GroupName = SiteGroupNames.Specialized, Order =10, Description = "The home page for a website with an area for blocks and partial pages")]
[SiteStartIcon]
public class StartPage : SitePageData
{
[CultureSpecific]
[Display(Name = "Heading", Description = "If the Heading is not set, the page falls back to showing the Name.",
GroupName = SystemTabNames.Content, Order =10)]
public virtual string Heading { get; set; }
[CultureSpecific]
[Display(
Name = "Main body",
Description = "The main body will be shown in the main content area of the page, using the XHTML-editor you can insert for example text, images and tables.",
GroupName = SystemTabNames.Content,
Order = 20)]
public virtual XhtmlString MainBody { get; set; }
[Display(Name = "Main content area",
Description =" The main content area contains an ordered collection to content references, for example blocks, media assets and pages.",
GroupName = SystemTabNames.Content, Order =30)]
public virtual ContentArea MainContentArea { get; set; }
}
}
Im sorry for sending the wrong code, here is the StartPageController:
using AlloyTraining.Models.Pages;
using EPiServer;
using System.Web.Mvc;
namespace AlloyTraining.Controllers
{
public class StartPageController : PageControllerBase<StartPage>
{
public StartPageController(IContentLoader loader) : base(loader)
{ }
public ActionResult Index(StartPage currentPage)
{
return View(CreatePageViewModel(currentPage));
}
}
}
Hi Daniel,
Try to use ' var viewmodel = PageViewModel<StartPage>.Create(currentPage);' instead of ' var viewmodel = PageViewModel.Create(currentPage);' within the CreatePageViewModel method.
public class PageControllerBase<T> : PageController<T> where T : SitePageData
{
protected readonly IContentLoader loader;
public PageControllerBase(IContentLoader loader)
{
this.loader = loader;
}
public ActionResult Logout()
{
FormsAuthentication.SignOut();
return RedirectToAction("Index");
}
protected IPageViewModel<TPage> CreatePageViewModel<TPage>(
TPage currentPage) where TPage : SitePageData
{
var viewmodel = PageViewModel<StartPage>.Create(currentPage);
viewmodel.Startpage = loader.Get<StartPage>(ContentReference.StartPage);
viewmodel.MenuPages = FilterForVisitor.Filter(
loader.GetChildren<SitePageData>(ContentReference.StartPage))
.Cast<SitePageData>().Where(page => page.VisibleInMenu);
viewmodel.Section = currentPage.ContentLink.GetSection();
return viewmodel;
}
}
Hi Sanjay, thank you for replying.
I've tried your solution, if I change to 'var viewmodel = PageViewModel<StartPage>.Create(currentPage), then the editor gives me red line errors on "viewmodel.Startpage, viewmodel.MenuPages, viewmodel.Section". When I hoover these errors , it says: 'object' does not contain a definition for (startpage, menupages, section) and no accessible extension method (startpage, menupages, section) accepting a first argument of type 'object' could be found(are you missing directive or an assembly reference?)
I'm not good at using debugging tool, but is there something here you can see where I can look at "Locals" section?
What happens if you change line 5 to this:
@(Model.CurrentPage != null ? (Model.CurrentPage.Heading ?? Model.CurrentPage.Name) : "")
Hi Tomas!
When I change line 5, it still shows error (look at picture below):
Hi, I'm following excerise for setting up EPiserver webpage and I've followed every steps and there no red lines errors on the code editor, but when I'm testing the web page it shows error.
Line 3: @model PageViewModel<AlloyTraining.Models.Pages.StartPage> Line 4: <h1 @Html.EditAttributes(m => m.CurrentPage.Heading)> Line 5: @(Model.CurrentPage.Heading ?? Model.CurrentPage.Name) Line 6: </h1> Line 7: <div>
Source File: c:\Episerver\CMSTraining\AlloyTraining\Views\StartPage\index.cshtml Line: 5
Stacktrace:
string str = null;
str.ToUpper();
Above c# code throws a NullReferenceException at the second line because you can't call the instance method ToUpper() on a string reference pointing to null. So, check your code once again.
http://net-informations.com/faq/net/interview-questions.htm