Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the Name of a block in EpiServer?

Tags:

episerver

How do I get the name, set in the CMS, of a certain block in an MVC view?

I guess that it should be @Model.Name but I can't find it.

like image 968
rablentain Avatar asked Sep 02 '16 08:09

rablentain


3 Answers

You have to cast your block instance to IContent to access the Name property.

For details on why, you can have a look at: Episerver - Why BlockData doesn't implement IContent

like image 64
Ted Nyberg Avatar answered Sep 23 '22 22:09

Ted Nyberg


The syntax to get the Name property is

(Model as IContent).Name

or

((IContent)Model).Name

Be careful with this cast as handling a Block which is a property as opposed to a ContentReference will not work and throws an exception.

like image 28
Robin French Avatar answered Sep 24 '22 22:09

Robin French


If you want to display the name in the view - you can cast the model inside PropertyFor: @Html.PropertyFor(m => ((IContent)m).Name)

like image 37
Linus Ekström Avatar answered Sep 25 '22 22:09

Linus Ekström