Page Type Property Problem

Corbin Camp
Member since: 2012
 

I have Page Type called StartPage.

public class StartPage: PageData

         [Editable(true)]
         [Display(
             Name = "Title",
             Description = "The page's title",
             GroupName = SystemTabNames.Content,
             Order = 1)]
         public virtual String Title { get; set; }


         [Display(Name = "Content Area",
             Description = "A  block goes here",
             GroupName = SystemTabNames.Content,
             Order = 2)]
         public virtual ContentArea ContentArea { get; set; }

}

Layout:

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>@ViewBag.Title</title>
</head>
<body>
    <h2>My Site</h2>
    <div>
        @RenderBody()
    </div>
</body>
</html>

Here is my Start View

@using EPiServer.Core
@using EPiServer.Web.Mvc.Html

@model Site.Models.Pages.StartPage

@{
        Layout = "~/Views/_Layout.cshtml";
}

<div>
@Html.PropertyFor(m => m.Title)
</div>
<div>
@Html.PropertyFor(m => m.ContentArea)
</div>

When I go to edit my page, I get this:

The model item passed into the dictionary is of type 'System.String', but this dictionary requires a model item of type 'Site.Models.Pages.StartPage'

And the YSOD is highlighting the @Html.PropertyFor(m => m.Title). If I change it to use @Model.Title instead, I can't edit on page, but it doesn't error out.
This doesn't happen if I have the Title property typed as an XhtmlString. The ContentArea renders fine and lets me drop a block onto it.
Any ideas?


#64238 Dec 13, 2012 22:58
  •  

    What happens if you do @Html.DisplayFor(m => m.Title)? If you get the same problem then for some reason the display template for strings is resolved to a view that has StartPage as model.

    Either way, unless you have a custom display template for strings your Title property will have different rendering in view and edit mode may not be what you want. Instead you can do something like <p @Html.EditAttributes(x => x.Title)>@Model.Title</p>

    #64240 Edited, Dec 14, 2012 0:16
  • Corbin Camp
    Member since: 2012
     

    DisplayFor didn't work either. The EditAttributes did work. Interresting as I never would have thought to use that. Why is or how is that working?

    #64320 Dec 17, 2012 19:29
  •  

    As Joel wrote, DisplayFor (and PropertyFor becouse PropertyFor only is a wrapper for DisplayFor to support on page edit) will look for a display template named string which takes string as the model. It seems to me that you have a string partial view that takes the pagetype as model under view->shared->displaytemplates.

    #64324 Dec 18, 2012 9:05
  •  

    Corbin, EditAttributes returns a string with HTML attributes which marks the element they are added to as the container for a property. I've found that I rarely use PropertyFor and instead use EditAttributes as PropertyFor is more intrusive when rendering properties in (DOPE) edit mode as it wraps them in a element which isn't rendered in view mode.

    #64326 Dec 18, 2012 9:25
  • Corbin Camp
    Member since: 2012
     

    I guess I would have thought ES7 would have handled string properties inherently. I didn't set up any template or partial view. Joel, interesting that you go with the EditAttributes when PropertyFor is used in the default templates (granted the default Page Type property is an XhtmlString). I have a lot of places I'm going to use this. Thanks for the help.

    #64381 Dec 18, 2012 17:23
  • javafun
    Member since: 2012
     

    what's your EPiserver version installed? are you using the preview version?

    #64424 Dec 20, 2012 13:07
  • Corbin Camp
    Member since: 2012
     

    I'm using the release version of 7.

    #64497 Dec 21, 2012 22:25
  •  

    Corbin, this is a really, really far fetched and wild guess, but could it be that you only experience this when Title is null, or vice versa, only when it's not null? 

    #64498 Edited, Dec 21, 2012 23:24