Try our conversational search powered by Generative AI!

Custom additionalViewData

Vote:
 

The XML comments for the @Html.PropertyFor says that the anonymous object you pass get's merged in to the ViewData dictionary. I tried passing my own object in to this parameter hoping it would be in my controller in the ViewData collection but nothing is there. Is there anyway to get this data back out again as I need to pass an object through to the block controller to change how it renders for the different places it's used?

#140436
Oct 20, 2015 10:00
Vote:
 

To answer your question, yes you should be able to retrieve the view data.

For instance, given a ContentArea named RightColumnArea and an additional view data with a string named "Placement":

@Html.PropertyFor(m => m.CurrentPage.RightColumnArea, new {Placement = "RightColumn"})

And in your block controller:

public override ActionResult Index(SomeBlock currentBlock)
{
	var placement = ControllerContext.ParentActionViewContext.ViewData["Placement"] as string ?? String.Empty;
	
	string viewName;
	switch (placement)
	{
		case "RightColumn":
			viewName = "RightColumn";
			break;
		default:
			viewName = "Index";
			break;
	}
	
	return PartialView(viewName, currentBlock);
}

That being said, there are other ways to achieve this.
If a block doesn't need any logic it should not have a controller due to performance, if this is your case then have a look at the TemplateCoordinator in AlloyTech.
If you need a controller, you could take a look at the TemplateDescriptor attribute.

Both the TemplateCoordinator class and TemplateDescriptor attribute uses something called Tag, which also makes selecting different views view DisplayOptions possible.

#140574
Oct 22, 2015 20:41
Vote:
 

Thanks I'm already using  the TemplateCoordinator for rendering our standard page partials and display options for the different adaptive versions of the blocks. This requirements comes from a necessity to be able to adapt the blocks furthur for dynamic resizing information which needs to be applied through from the view dependant on when the block is used which the additonalViewData seemed perfect for. I'll try your approach and see if it does the job.

Thank you for the help

#140579
Oct 23, 2015 9:54
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.