Pages

Wednesday, April 6, 2016

Episerver Block Controller error

I am expanding my horizons in the CMS world to add Episerver to the list of platforms I develop on, and the process is a bit of trial-and-error, with an emphasis on the "error" part the past couple weeks. I am coming into this without attending any Episerver training, so this may be known to everyone else, but, nevertheless, I found this error rather intriguing and wanted to share in case someone else missed the cause due to lack of coffee.

I am building a site using MVC, and with that comes the creation of a block class, and a block controller. I added my block to my Page model and then proceeded to create the controller. Using the standard approach in Episerver, I named the controller and block to match, with the block called CalloutBlock, and the matching controller CalloutBlockController. After copying some code into my controller, building, and refreshing my page, I was greeted with the following, lovely message:

"The routed data is of type 'Tradeweb.Models.Pages.HomePage' and the binding context 'currentPage' is null, cannot bind any value to type 'Tradeweb.Models.Blocks.CalloutBlock'."

The Problem

This might be obvious to some right from the start, but it didn't jump out to me right away, and for that I blame coffee, or lack thereof. Right in the error message, however, it points out that the "the binding context 'currentPage' is null" and I should have picked up on that. 

I am working with a Block Controller, which should not reference "currentPage", but the code I copied and pasted over my Action method was copied from a Page Controller, and I wasn't paying attention. It turns out, if your parameter is named "currentPage", Episerver tries to bind the page context to the controller, which won't work since I am expecting a CalloutBlock type.
[pre class="brush:csharp" title="Oops"]
public class CalloutBlockController : BaseBlockController<CalloutBlock> {
    public override ActionResult Index(CalloutBlock currentPage) {
        return PartialView();
    }
}
[/pre]

The Solution

It's a pretty simple solution to fix because all that is needed is a different name for the parameter. I was comparing to my other Block Controllers, but it didn't jump out at me for a few minutes. It's kind of like those spelling or grammar errors in your articels you don't notice, but someone else reading picks up on immediately. 

A quick name change to "currentBlock" because, well, it's just obvious and every other Block Controller uses that name by default, followed by a fresh build, and it's all back together again.
[pre class="brush:csharp" title="TaDa!"]
public class CalloutBlockController : BaseBlockController<CalloutBlock> {
    public override ActionResult Index(CalloutBlock currentBlock) {
        return PartialView();
    }
}
[/pre]
Maybe this is something I would have known from the start if I had training already. Maybe it should have been obvious. At least if you have come here because you came across it, maybe I saved you a bit of time.

No comments:

Post a Comment

Share your thoughts or ask questions...