MVC Dynamic Property question

 

I'm starting a new project with version 7, and we are trying to follow ideas presented in the Alloy Tech demo site(MVC and Razor mostly). However the Alloy site does not use Dynamic Properties, at least as far I can see. What is the best way to access these properties with Razor?

How would we access Dynamic Properties if the Model does not have knowledge of it? Should we define these properties in the Model as well?

 

Thanks.

#65722 Feb 06, 2013 16:07
  • Johan Björnfot
    Member since: 2004
     

    You can have dynamic properties as typed properties on your model. You should then mark them with Ignore atttribute otherwise it will be created a page property with same name. Like below:

    [Ignore]
    public virtual XhtmlString DynamicHtml { get; set; }

    It is however not possible to create/define dynamic properites on your models so you need to create them in admin mode. Then in your view you can render them as :

     Html.DisplayFor(m => m.DynamicHtml)

    Note: you will not get onpageedit support on your dynamic properties, the need to be edited in the separeate dynamic properties dialog

    #65726 Feb 06, 2013 17:39
  •  

    Thanks, it is exactly what I was looking for.

    #65729 Feb 06, 2013 21:22
  • Minesh Shah
    Member since: 2007
     

    I have used this logic in my 7.5 solution and and getting a null when trying to access the dynamic property

            [Ignore]
            public virtual PageReference SiteConfigurationPage { get; set; }

    While debugging i get the following

    Model.CurrentPage["SiteConfigurationPage"]
    {9}
        base: {9}
        RemoteSite: null
    
    Model.CurrentPage.SiteConfigurationPage
    null

    Any idea on what i might be doing wrong ?

    #90722 Sep 16, 2014 18:41
  •  

    In CMS 7.5 to achieve the same functionality you need to implement the dynamic property like this: 

    [Ignore]
    public virtual string DynamicPropertyName
            {
                get
                {
                    var dynamicProp = Property["DynamicPropertyName"];
                    return dynamicProp != null ? dynamicProp.Value as string : null;
                }
                
            }
    #90779 Edited, Sep 17, 2014 17:06
  • Komal Goyani
    Member since: 2014
     

    I have created Dynamic Property in Admin mode but following code always written null reference exception in EPIserver7.5.

       PropertyData property = CurrentPage.Property["SiteConfigurationPage"];

    Even i also tried with your solution : 

    [Ignore]

    public virtual string SiteConfigurationPage

    { get

       { var configPage = Property["SiteConfigurationPage"];

           return configPage != null ? configPage.Value as string : null; }

    }

    but same error..

    Any ideas?

    #109724 Oct 13, 2014 17:00
  • Minesh Shah
    Member since: 2007
     

    Komal, make sure the name of the property you created via admin mode and the magic string in code are the same,

    #109725 Oct 13, 2014 17:03
  • Komal Goyani
    Member since: 2014
     

    Worked!!  Thanks minesh..

    #109726 Oct 13, 2014 17:37