Dynamically create a block in a non EPiServer page

Vote:
 

Dear community,

For a special purpose, I need to create a block each time I render a page (I need to reuse an EPiServer block in a non EPiServer page). This is fairly easy to do with a view model. Let's assume my view model is defined as:

public class ViewModel
    {
        public MyBlock Block
        {
            get
            {
               return new MyBlock 
                {
                    // Some predefined properties are set here
                };
            }
        }
    }

In the code above, the view model exposes a new instance of MyBlock, that inherits from BlockData. Finally, I render the block in my view:

@Html.PropertyFor(m => m.Block)

Do you see any issue of doing that ? Does it create unnecessary reference in the database each time I create a new instance of a class that inherits from BlockData ?

Many thanks !

Kind Regards,

Jämes

#176550
Mar 21, 2017 11:35
Vote:
 

Hi Jämes,

If you really have to use an Episerver block this will still have to exist somewhere, most likely in a dedicated shared Block folder.

It sounds to me though like there is something in the presentation of the block you are wanting to reuse. If the block does not need an editor to enter content, it is probably better that you just create a partial view or user control that is then rendered on the page.

#176739
Mar 24, 2017 10:52
* 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.