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

3

5

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.

Congresswoman answered 2/9, 2016 at 8:11 Comment(1)
Were you able to get the block name based on the suggested answer?Relentless
R
8

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

Relentless answered 2/9, 2016 at 12:56 Comment(0)
M
5

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.

Moitoso answered 12/7, 2018 at 12:34 Comment(5)
Good point! I think your first example should be var blockName = (Model as IContent)?.Name ?? "Local block" to avoid a potential null reference exception. To use the null-coalescing operator in a Razor view you need to enable C# 6+ with the Microsoft.CodeDom.Providers.DotNetCompilerPlatform NuGet package, though.Relentless
Yeah, you are right about that. I do find though that the cast exception tends to counter the risk of null reference exception. The idea is that if the object was null after the cast then the cast itself would have thrown an exception. For completeness though you're absolutely right and there should be a null check on it. ReSharper would put a possible null reference warning on that line without one. It's a shame you can't use ? operator in razor without that package as it makes the null checks much tidier!Moitoso
It's weird that you have to go to these lengths to get the block name as well. Especially since using the block name is such a useful fall back to avoid putting a requirement on a title field so it can be left blank and the title defaults to the block name.Moitoso
I think the architecture makes sense, but I see what you mean. We derive most of our block types - especially those used in content areas - from a custom base class with a Name property, so that it's accessible without the extra hassle. :)Relentless
That would be very useful but I guess the issue is you can't get the Name without tying the object to IContent, which is what they were trying to avoid. I'm considering an extension method for a base block type to return the block as IContent since I seem to be writing this bit of code quite a lot!Moitoso
J
0

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

Jard answered 11/11, 2019 at 15:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.