Thursday, March 20, 2014

Posting from an EPiServer MVC block

When Posting to a custom Action, we go outside the normal EPiServer context, so EPiServer cannot resolve a "currentBlock" parameter or current language.
 @using (Html.BeginForm("SomeAction", "MyBlock"))   
 {   
   <input type="submit" />  
 }

The Action URL will be something like "/MyBlock/SomeAction", which is different from the current page URL.
We need to manually redirect the browser back to the current page URL. For example by maintaining a CurrentPageLink and CurrentLanguage in the view model.
 [HttpPost]  
 public ActionResult SomeAction(MyViewModel viewModel)  
 {   
   return new RedirectResult(_urlResolver.GetUrl(viewModel.CurrentPageLink, viewModel.CurrentLanguage));  
 }  
This can be simplified by using language specific forms.