November Happy Hour will be moved to Thursday December 5th.

The model item passed into the dictionary is of type 'Castle.Proxies.StartPageProxy', but this dictionary requires a model item of type 'x'

Vote:
 

Hi all, I'm a beginner just learning how to use Episerver and I was doing the free online training course and after doing one particular exercise I keep getting this error when I go to the start page of my episerver site:

The model item passed into the dictionary is of type 'Castle.Proxies.StartPageProxy', but this dictionary requires a model item of type 'AlloyTraining.Models.ViewModels.PageViewModel`1[AlloyTraining.Models.Pages.StartPage]'.

The edit page works, but the live view does not.   Anyone know how to fix this?

Relevant files:
...\AlloyTraining\Views\StartPage\Index.cshtml

@using AlloyTraining.Models.ViewModels
@using EPiServer.Web.Mvc.Html
@model PageViewModel<AlloyTraining.Models.Pages.StartPage>
<h1 @Html.EditAttributes(m => m.CurrentPage.Heading)>
    @(Model.CurrentPage.Heading ?? Model.CurrentPage.Name)
</h1>
<div>
    @Html.PropertyFor(m => m.CurrentPage.MainBody)
</div>
<div>
    @Html.PropertyFor(m => m.CurrentPage.MainContentArea)
</div>

...\AlloyTraining\Models\Pages\StartPage.cs

using System;
using System.ComponentModel.DataAnnotations;
using AlloyTraining.Views;
using EPiServer.Core;
using EPiServer.DataAbstraction;
using EPiServer.DataAnnotations;
using EPiServer.SpecializedProperties;

namespace AlloyTraining.Models.Pages
{
    [ContentType(DisplayName = "StartPage", 
        GroupName = SiteGroupNames.Specialized,
        Order = 10,
        GUID = "fd956a78-f398-42bd-a9c0-8bf79d5df96c", 
        Description = "The home page for a webiste with an area for blocks and partial pages")]
    [SiteStartIcon]
    public class StartPage : SitePageData
    {
        
        [CultureSpecific]
        [Display(
            Name = "Heading",
            Description = "If the headeing is not set, the pages falls aback 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; }

        [CultureSpecific]
        [Display(
            Name = "Main Content Area",
            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 = 30)]
        public virtual XhtmlString MainContentArea { get; set; }


        [CultureSpecific]
        [Display(Name = "Footer text",
            Description = "The footer text will be shown at the bottom of every page.",
            GroupName = SiteTabNames.SiteSettings, Order = 10)]
        public virtual string FooterText { get; set; }

    }
}

...\AlloyTraining\Controllers\PageControllerBase.cs

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 abstract 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;
        }
    }
}

...\AlloyTraining\Models\Pages\StartPageController.cs

using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using AlloyTraining.Models.Pages;
using EPiServer;
using EPiServer.Core;
using EPiServer.Framework.DataAnnotations;
using EPiServer.Web.Mvc;

namespace AlloyTraining.Models.Pages
{
    public class StartPageController : PageController<StartPage>
    {
        public ActionResult Index(StartPage currentPage)
        {
            /* Implementation of action. You can create your own view model class that you pass to the view or
             * you can pass the page type for simpler templates */

            return View(currentPage);
        }
    }
}

Not really sure what file is messing things up or where to look.


Here is the full error if that helps:

Server Error in '/' Application.
The model item passed into the dictionary is of type 'Castle.Proxies.StartPageProxy', but this dictionary requires a model item of type 'AlloyTraining.Models.ViewModels.PageViewModel`1[AlloyTraining.Models.Pages.StartPage]'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: The model item passed into the dictionary is of type 'Castle.Proxies.StartPageProxy', but this dictionary requires a model item of type 'AlloyTraining.Models.ViewModels.PageViewModel`1[AlloyTraining.Models.Pages.StartPage]'.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:


[InvalidOperationException: The model item passed into the dictionary is of type 'Castle.Proxies.StartPageProxy', but this dictionary requires a model item of type 'AlloyTraining.Models.ViewModels.PageViewModel`1[AlloyTraining.Models.Pages.StartPage]'.]
   System.Web.Mvc.ViewDataDictionary`1.SetModel(Object value) +175
   System.Web.Mvc.ViewDataDictionary..ctor(ViewDataDictionary dictionary) +107
   System.Web.Mvc.WebViewPage`1.SetViewData(ViewDataDictionary viewData) +49
   System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +99
   System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +107
   System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +291
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +13
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +56
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +420
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +420
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +420
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +420
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +420
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +420
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +52
   System.Web.Mvc.Async.<>c__DisplayClass3_6.<BeginInvokeAction>b__4() +198
   System.Web.Mvc.Async.<>c__DisplayClass3_1.<BeginInvokeAction>b__1(IAsyncResult asyncResult) +100
   System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +27
   System.Web.Mvc.<>c.<BeginExecuteCore>b__152_1(IAsyncResult asyncResult, ExecuteCoreState innerState) +11
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +29
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +45
   System.Web.Mvc.<>c.<BeginExecute>b__151_2(IAsyncResult asyncResult, Controller controller) +13
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +22
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +26
   System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10
   System.Web.Mvc.<>c.<BeginProcessRequest>b__20_1(IAsyncResult asyncResult, ProcessRequestState innerState) +28
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +29
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +28
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +577
   System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step) +132
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +163
#223440
May 27, 2020 23:39
Vote:
 

Your view expects the model to be `PageViewModel<AlloyTraining.Models.Pages.StartPage>`  but in your controller, you are passing `AlloyTraining.Models.Pages.StartPage`.

Could you try changing your Controller to this?

public ActionResult Index(StartPage currentPage)
{
   var model = PageViewModel.Create(currentPage);
   return View(model);
}
#223453
Edited, May 28, 2020 6:17
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.