Hey Kiril, this blog post might be useful for what you are trying to achieve... Retrieving absolute url of current page on any page or block (optimizely.com)
Hi Kiril,
This sounds very similar to an issue I had with getting standard MVC forms to work on a block. You may want to post back to the block's controller, but that controller doesn't have a concept of which page the block is currently hosted. I solved this by doing the following:
using System.Web.Mvc;
using EPiServer.Core;
using EPiServer.Web.Mvc;
using EPiServer.Web.Routing;
public class FormBlockController : BlockController<FormBlock>
{
private readonly IPageRouteHelper _pageRouteHelper;
public FormBlockController(IPageRouteHelper pageRouteHelper)
{
_pageRouteHelper = pageRouteHelper;
}
public override ActionResult Index(FormBlock currentContent)
{
var blockReference = (currentContent as IContent)?.ContentLink;
var hadForm = LoadModelState(blockReference);
var formSubmitModel = GetFormSubmitModel();
var model = new FormBlockViewModel
{
CurrentPageReference = _pageRouteHelper.Page.ContentLink
};
if (ModelState.IsValid && hadForm)
{
// Do Submit state stuff
}
return PartialView("~/Views/Shared/Blocks/FormBlock.cshtml", model);
}
[HttpPost]
public ActionResult Submit(FormSubmitModel formModel)
{
SaveModelState(formModel.CurrentBlockReference);
return RedirectToAction("Index", new { node = formModel.CurrentPageReference, language = formModel.CurrentLanguage });
}
private FormSubmitModel GetFormSubmitModel()
{
return new FormSubmitModel
{
PropOne = GetAttemptedValue(nameof(FormSubmitModel.PropOne))
};
}
private string GetAttemptedValue(string modelStateKey)
{
if (ModelState != null && ModelState.ContainsKey(modelStateKey))
{
return ModelState[modelStateKey].Value.AttemptedValue;
}
return string.Empty;
}
private void SaveModelState(ContentReference blockReference)
{
TempData[StateKey(blockReference)] = ViewData.ModelState;
}
private bool LoadModelState(ContentReference blockReference)
{
var key = StateKey(blockReference);
var hadFormModelState = false;
if (TempData.ContainsKey(key) && TempData[key] is ModelStateDictionary modelState)
{
ViewData.ModelState.Merge(modelState);
TempData.Remove(key);
hadFormModelState = true;
}
return hadFormModelState;
}
private string StateKey(ContentReference blockReference)
{
var blockId = blockReference?.ID ?? -1;
return $"FormBlock_{blockId}";
}
}
Hi Kiril
The latest version of the v11 CMS UI has a new service called CurrentContentContext which allows you to work out the current content so would allow you to work out what page is rendering your block.
There is a nice blog about it here: https://world.optimizely.com/blogs/bartosz-sekula/dates/2021/9/how-to-resolve-current-content-context/
David
I have block controller and custom action for filtering data after I filter the data I want to redirect back to the Index with the filtered data and render the block. How can I do this? Currently im redirecting back to "/" however I want to redirect to where the current block is. I tried Redirect(nameof(Index)) and I get 404.