Inheriting other PageType property values

Vote:
 

Hi, what I'm trying to build is a custom invitationMessage property that will be sent when someone is invited to an event, for example Facebook has something like "You've been invited to *Event* by *User* ~something", but I need the value of this property on several pages, so I thought maybe I can set the values on the StartPage and somehow pass it on to the children of the StartPage.
I, however, have no idea how to achieve this.

Here's my code for the properties:

 [Required]
        [CultureSpecific]
        [Display(
            Name = "Invitation Message Title",
            Description = "Title of the message that will be sent to invited users. Use [EventName] or [User] tag if you want them to be a part of the title.",
            GroupName = SystemTabNames.Content,
            Order = 200)]
        public virtual string InvitationMessageTitle { get; set; }

        [Required]
        [CultureSpecific]
        [Display(
            Name = "Invitation Message",
            Description = "The message that will be sent when a user invites others to their event. Always specify a part in the message which contains these tags '[EventName]' or '[User]' if you want to link to the specific event and user in the message.",
            GroupName = SystemTabNames.Content,
            Order = 210)]
        public virtual string InvitationMessage { get; set; }


I've tried inheriting the data of the other PageType like so: ProfilePage : Startpage, but this only inherits the same properties without values.

Any ideas of how I can achieve this?

#91655
Oct 10, 2014 9:52
Vote:
 

I usally add a reference to the startpage on the basepage used for all pagetypes in the prodject. That way you can always get the settings added to the startpage.

/// <summary>
/// Base class for all page types
/// </summary>
public abstract class BasePageData : PageData
{

/// <summary>
/// Returns the Start page
/// </summary>
public StartPage StartPage
{
get
{
return DataFactory.Instance.Get<StartPage>(ContentReference.StartPage);
}
}

....

}

#109632
Oct 10, 2014 16:12
Vote:
 

I'm sorry, but I don't quite understand.
Would you mind elaborating your explanation a little?

What is this basepage you're talking about?
Do I have to create it, and if so - how?

#109668
Oct 13, 2014 9:33
Vote:
 

The basepage is a class I create so yes you will have to add your own. 

Just add a code file to your prodject and let it inheret from PageData. Then on this you could add all property definitions that will be used on all pages. Then let the pagetypes that you create inheret from the created basepage.

#109693
Oct 13, 2014 12:11
Vote:
 

I would recomend you to install the alloy template site and have a look at the setup in there. They use the SitePageData as their base class for all pagetypes.

#109694
Oct 13, 2014 12:13
Vote:
 

I think what Petter is referring to is the assumption that the properties live solely on the StartPage, which in my opinion, is the correct way to handle this, assuming that the property values do not change per each page instance.

If that is the case, keep the properties on the StartPage, and don't let any other pages inherit from the StartPage, then you can access those properties using the code that Petter provided (e.g. CurrentPage.StartPage.InvitationMessageTitle)

If the property values need to be different per each page instance, then you could inherit from the StartPage, but I think it would be better to inherit from a different base class, or even just create an interface, since the StartPage is typically a unique page type. In my projects, I never inherit from the StartPage.

#109695
Oct 13, 2014 12:16
Vote:
 

Okay, I'm still not quite sure how to do this, I'll try to explain what I want to do better.

This is one of the ViewModels of a different PageType I want to populate with values inherited from the StartPage / SitePageData:

public AdsPageViewModel(AdsPage currentPage)
        {

        }

        public string InvitationMessage { get; set; }
        public string InvitationMessageTitle { get; set; }

And my AdsPage also inherits from SitePageData:

    [ContentType(DisplayName = "AdsPage", GUID = "93f82d57-f1b8-473f-9ba1-14843f10b8c5", Description = "")]
    public class AdsPage : SitePageData



This is my newly created SitePageData based on the code above, not sure if it's correct:

public class SitePageData : PageData
    {

        public StartPage StartPage
        {
            get
            {
                return DataFactory.Instance.Get<StartPage>(ContentReference.StartPage);
            }
        }


    }


My StartPage with the properties I want:

[ContentType(DisplayName = "StartPage", GUID = "061a1365-7fd6-4f72-ad76-4e8f349029ba", Description = "")]
    public class StartPage : SitePageData
    {
        [CultureSpecific]
        [Display(
            Name = "Content Area",
            GroupName = SystemTabNames.Content,
            Order = 100)]
        public virtual ContentArea ContentArea { get; set; }

        [CultureSpecific]
        [Display(
            Name = "Main Body",
            GroupName = SystemTabNames.Content,
            Order = 200)]
        public virtual XhtmlString MainBody { get; set; }

        [Required]
        [Display(
            Name = "Login redirect URL",
            Description = "Specify which page you want your user to be redirected to after signing in.",
            Order = 300
            )]
        public virtual PageReference LoginRedirect { get; set; }

        [Required]
        [Display(
            Name = "Facebookbutton text",
            Description = "The text that will be displayed on the 'Sign in with facebook'-button.",
            Order = 400
            )]
        public virtual string FacebookButtonText { get; set; }

        [Required]
        [CultureSpecific]
        [Display(
            Name = "Invitation Message Title",
            Description = "Title of the message that will be sent to invited users. Use [EventName] or [User] tag if you want them to be a part of the title.",
            GroupName = SystemTabNames.Content,
            Order = 500)]
        public virtual string InvitationMessageTitle { get; set; }

        [Required]
        [CultureSpecific]
        [Display(
            Name = "Invitation Message",
            Description = "The message that will be sent when a user invites others to their event. Always specify a part in the message which contains these tags '[EventName]' or '[User]' if you want to link to the specific event and user in the message.",
            GroupName = SystemTabNames.Content,
            Order = 600)]
        public virtual string InvitationMessage { get; set; }

        public override void SetDefaultValues(ContentType contentType)
        {

            base.SetDefaultValues(contentType);

            LoginRedirect = PageReference.StartPage;
            FacebookButtonText = "Logga in med";
        }

    }


I want the InvitationMessage and InvitationTitle properties to be available on all/multiple PageTypes with inherited values which the editor sets in the StartPage, this is because I want the same value on several Pages which without having the editor type them in on each PageType as this could end with different values etc.

#109699
Oct 13, 2014 12:57
Vote:
 
public AdsPageViewModel(AdsPage currentPage)
{
 
 
       public string InvitationMessage 
       { 
         get
         {
           currentPage.StartPage.InvitationMessage
         }
       }
....
}

This should give you the value from the startPage.

#109700
Oct 13, 2014 13:05
Vote:
 

So in your controller, where you create your instance of the AdsPageViewModel, populate the view model's properties with those set on the StartPage:

public ActionResult Index(AdsPage currentPage)
{
	var model = new AdsPageViewModel 
	{
		InvitationMessage = currentPage.StartPage.InvitationMessage,
		InvitationMessageTitle = currentPage.StartPage.InvitationMessageTitle
	};

	return View(model);
}
#109701
Oct 13, 2014 13:05
Vote:
 

Not sure why but seems my code got stripped.

public AdsPageViewModel(AdsPage currentPage)
{
 
        
 
        public string InvitationMessage
       {
         get
         {
           return currentPage.StartPage.InvitationMessage;
         }
       }
.....
}
#109702
Oct 13, 2014 13:07
Vote:
 

Thanks a lot you guys, now it works perfectly!

Regards,
Ludvig Flemmich

#109705
Oct 13, 2014 13:44
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* 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.