How to render a block differently in the cms editor to the websites webpage?

Vote:
 

If you create a simple new block, with a few properties, we want to show just a single line of text in the editor, but for the website, we want to generate JSON.

Currently, we havce a single view in /Views/Shared/Blocks/ItemBlock.cshtml:

@model DxcAlloy.Models.Blocks.ItemBlock

{
"id":@Html.PropertyFor(x => x.id) ,
"name":"@Html.PropertyFor(x => x.name)"
}

However, for the editor view, we just want the name, such as:

@model DxcAlloy.Models.Blocks.ItemBlock

@Html.PropertyFor(x => x.name)

Is there a way to have different views for the editor and for the actual web page?

Interestingly, even with the above very simple view, its quite different from website to editor. Website shows it like this:

{ "id":123 , "name": "item name" }

But editor shows it like this:

{ "id":

123

, "name": "

item name

" }

#224458
Edited, Jun 20, 2020 9:05
Vote:
 

Maybe this feels hacky, I do not know. Guess it should work.

@model DxcAlloy.Models.Blocks.ItemBlock

@if (PageEditing.PageIsInEditMode)
{
   @Html.PropertyFor(x => x.name)
}
else
{
    @Html.Raw("{\"id\":" + Model.id + ", \"name\":\"" + Model.name + "\"}")
}
#224505
Jun 22, 2020 5:12
* 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.