Don't miss out Virtual Happy Hour this Friday (April 26).

Try our conversational search powered by Generative AI!

Display simple property value in edit mode

Vote:
 

I thought this would be an easy task but im stuck.

In my model I created a simple MonthReminder property.

        [Display(
            Name = "Months",
            Description = "Number of months to be reminded",
            Order = 200,
            GroupName = Tabs.Info)]
        [Range(1, 12)]
        public virtual int MonthReminder { get; set; }

Nothing fancy and works fine but then I want to display the Changed date + the MonthReminder somewhere on the page.

With Changed date I mean the built in property that epi sets when modifiy the page

I tried this but don´t show anything on the page.

        [Display(Name = "Readonly Property",
            GroupName = Tabs.Info,
            Description = "The date to be reminden")]
        [Editable(false)]
        public virtual DateTime DateReminder{ get => Changed.AddMonths(MonthReminder); }

I do not even manage to show a value on the page, either MonthReminder or Changed by it´s own.

We use EpiServer 11.

Thanks.

#274496
Edited, Feb 24, 2022 7:17
Vote:
 

Hi roYal, 

I Don't think you can do this like that, what can you do to add this to your controller not to model file and show it on cshtml after adding,

// in model
  [Ignore]
  public DateTime DateReminder { get; set; }
 
// in controller
  currentPage.DateReminder = currentPage.DateReminder;

// in cshtml
@{
adding both in variable and show it.
}

thank you
#274539
Feb 25, 2022 7:21
Vote:
 

Thank you for answering PuneetGarg.

As I understand it, I have to create a widget if I want to display a property value in edit mode.

Otherwise I have to present the value in the view instead of in the edit mode?

I saw in the settings tab where published date shows as the image below, is it complicated to manage?

Thanks.

#274540
Edited, Feb 25, 2022 7:55
Vote:
 

I sorted it out.

Created a new Initialization class

[InitializableModule]
    [ModuleDependency(typeof(EPiServer.Web.InitializationModule))]
    public class EventInitialization : IInitializableModule
    {
        public void Initialize(InitializationEngine context)
        {
            ServiceLocator.Current.GetInstance<IContentEvents>().LoadedContent += eventRegistry_LoadedContent;
        }

        public void Uninitialize(InitializationEngine context)
        {
            ServiceLocator.Current.GetInstance<IContentEvents>().LoadedContent -= eventRegistry_LoadedContent;
        }

        void eventRegistry_LoadedContent(object sender, ContentEventArgs e)
        {
            if (e.Content is MyPageData s)
            {
                s = s.CreateWritableClone() as MyPageData;
                s.DateReminder= s.Changed.AddMonths(p.MonthReminder);
                e.Content = s;
            }
        }
    }

 Then add the DateToRemind property in you Model to display the new date.

public virtual DateTime? DateReminder{ get; set; }

Done

#274547
Feb 25, 2022 13:39
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.