Block with and without controller

Vote:
 

I want to create a block with two or three properties. 

I just want to know the difference between a block with and without controller. A block created without controller has the view (with block name) under shared folder, however for the one with controller we will have a separate folder with Block Name and Index view in it.

Could anyone please list out which one is preferable and why?

#148463
May 13, 2016 11:15
Vote:
 

If you have simple blocks that just renders a few properties then avoid using a separate controller.


If you have plenty of logic that you need to take care of for the block, external data sources etc, then use a separate controller instead of making the poor views/html helpers have too much to worry about. 

So both are good ideas; Just depends on how advanced your block is...

#148465
Edited, May 13, 2016 11:24
Vote:
 

If you switch from non-controller approach to controller approach -> sometimes you may wonder why your controller is not hit. Remember that EPiServer is using conventions to register templates for each content type. If there will be "reachable" template with the same name as your block type name -> your controller most probably will not be hit and EPiServer will skip that -> going directly to template.

Also - it's not necessary to have folder with block name and index.cshtml inside. What we are usually doing for blocks (it's subjective choice) - we do have templates "_{block-name}.cshtml" (_MyBlock.cshtml). This will make sure that EPiServer is not picking up template automatically.

#148491
May 14, 2016 22:54
Vote:
 

From my memory, the block without controller has better performance. If your view only need to render that block data, personally, I would go with no controller approach. 

#148541
May 17, 2016 2:45
Vote:
 

It really depends on complexity of the block logic. Sometimes there is no way to avoid controller for the block. But yes, you are true - invoking controller for the block costs more that dumb generation of the renderer template for the block.

#148544
May 17, 2016 8:45
Vote:
 

Yup, better performance without controller and if you have a partial view you get perfect out of the box support for wysiwyg in edit mode as well. Otherwise you will need a little more tricks to get Ajax updates of blocks in edit mode to work...

#148545
May 17, 2016 8:45
Vote:
 

Question: if you don't have a controller for the block, when you try to view the block (and not the page) in edit mode, you get a YSOD.

Any way to resolve this?

#175055
Edited, Feb 09, 2017 23:23
Vote:
 

What says exception? I assume you have preview controller, right?

#175060
Feb 10, 2017 5:03
Vote:
 

Yeah I have a preview controller - just cracked it, it was because the view for my preview controller was in the wrong place

#175079
Edited, Feb 10, 2017 12:29
Vote:
 

Hi James,

Where was the view when it was in the wrong place, and where should it have been? I had trouble with preview before.

Marshall

#175083
Feb 10, 2017 14:11
* 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.