November Happy Hour will be moved to Thursday December 5th.

Accessing block data for custom action in block controller

Vote:
 

I have a block controller with an action that is mapped to a custom route:

public class MyBlockController : BlockController
{
	public override ActionResult Index(MyBlock currentBlock)
	{
		return View();
	}

	public string CustomRouteMethod(string parameter1)
	{
		// Is it possible to get an instance of MyBlock here? Since this method should be
		// invoked with AJAX, we can't post any models.
	}
}

In this action I want to access som block properties, but I can't figure out how to obtain an instance of the current block. Since the action should be invoked from JavaScript, the block can't be passed in as a parameter - instead it has to be resolved based on the current context somehow. 

Is this possible?

I've already tried the following without success:

  • PageRouteHelper: Content and ContentLink are always null, Page/PageLink always returns the start page.
  • HttpContext.Current.GetRequestContext().GetContentLink(): always returns null.
#133039
Aug 25, 2015 13:58
Vote:
 

Hi,

In theory from a javascript call (ajax), block is out of context. Think MVC Areas or a web api to fulfil your service requests.

Regards
/K

#133046
Aug 25, 2015 14:59
Vote:
 

That's not working out of the box, as next (you Ajax call to the server) request is out of context and has "lost connection" to original block.

One of the possible solutions (if your project's security policy allows that) you could read those properties from the block while rendering the markup for the inital request's response. Then add those values to the markup, pass them over to Javascript and use them in next Ajax call as parameters.

Another alternative (not so easy) - you would need to "restore" routing data for EPiServer to understand within which block Ajax call is happening. This is not so easy due to manipulation of RouteData objects and state to "convience" EPiServer to pick up correct block type / instance.

#133165
Aug 26, 2015 21:38
Vote:
 

I guessed as much... What I ended up doing was to render the block ID as a data attribute in the markup, and sending it as a parameter in the Ajax call. In the CustomRouteMethod() I then fetched the block using IContentRepository.

#133168
Aug 27, 2015 8:11
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* 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.