Try our conversational search powered by Generative AI!

Accessing name of block from view template

Vote:
 

I have created a block called SectionTitleBlock with the following model and view:

    public class SectionTitleBlock : EPiServer.Core.BlockData
    {
        public virtual string Title { get; set; }

[UIHint(Global.SiteUIHints.SectionIcon)] public virtual string Icon { get; set; } } public partial class SectionTitleBlockControl : BlockControlBase<SectionTitleBlock> { /// If the Title has not been set, return the name of the block as it was created in CMS. public string Title { get { if (String.IsNullOrWhiteSpace(this.CurrentData.Title)) { var content = this.CurrentData as IContent; if (content != null) return content.Name; } return this.CurrentData.Title; } } }

 

I want the Title property in the view to return the name of the block when the title has not been filled out by the user on the block.

I am trying to cast this.CurrentData to IContent to retrieve the name, however the cast results in null - looking at the inheritance of the classes and interfaces this looks correct not to cast correctly.

I can cast this.CurrentData to IContentData but that still does not provide me the name of the block as it was created in CMS.

 

Can anyone help?

 

Thanks,

Adam

#75869
Oct 09, 2013 12:53
Vote:
 

It seems strange if you get a NULL when you cast your CurrentData to IContent.

The instance of the Block is in runtime created as a Proxy Class with IContent as a "mixin". Hence there are no programmatically relations with your BlockData and the IContent.

Note that this only works when you use your Block as a Shared Block, not if you add the Block as a Local Block (a Property on a Page).

#75873
Oct 09, 2013 13:29
Vote:
 

Thanks Alf

I think that is the issue - I am using the block as a property on another block.

So as an example, I have a block called NewsList of which one of the properties is SectionBlockTitle.

From the Template of SectionBlockTitle, can I access the name of the parent NewsListBlock?

 

 

 

 

 

#75874
Oct 09, 2013 13:43
Vote:
 

Maybe not otpimal but you could maye load in block instance.

var repository = EPiServer.ServiceLocation.ServiceLocator.Current.GetInstance<IContentRepository>();
IContent block = repository.Get<IContent>(content.ContentLink);

block.Name;

#75885
Oct 09, 2013 16:17
Vote:
 

Per, what should "content" on your second row be?

In this case, the SectionTitleBlock is a local Block inside a Shared Block.

None the less, CurrentData on SectionTitleBlock cast to IContent will return null since it is a Local Block.

#75886
Oct 09, 2013 16:21
This thread is locked and should be used for reference only. Please use the Episerver CMS 7 and earlier versions forum to open new discussions.
* 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.