Merge pull request #1193 from umbraco/temp-U4-8220

U4-8220 ModelState and ViewData is not carried through when using the…
This commit is contained in:
Warren Buckley
2016-03-23 12:10:05 +00:00
2 changed files with 38 additions and 8 deletions

View File

@@ -21,20 +21,46 @@ namespace Umbraco.Web.Mvc
/// </remarks>
internal class EnsurePartialViewMacroViewContextFilterAttribute : ActionFilterAttribute
{
/// <summary>
/// Ensures the custom ViewContext datatoken is set before the RenderController action is invoked,
/// this ensures that any calls to GetPropertyValue with regards to RTE or Grid editors can still
/// render any PartialViewMacro with a form and maintain ModelState
/// </summary>
/// <param name="filterContext"></param>
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
//ignore anything that is not IRenderController
if ((filterContext.Controller is IRenderController) == false)
if ((filterContext.Controller is IRenderController) == false && filterContext.IsChildAction == false)
return;
SetViewContext(filterContext);
}
/// <summary>
/// Ensures that the custom ViewContext datatoken is set after the RenderController action is invoked,
/// this ensures that any custom ModelState that may have been added in the RenderController itself is
/// passed onwards in case it is required when rendering a PartialViewMacro with a form
/// </summary>
/// <param name="filterContext">The filter context.</param>
public override void OnResultExecuting(ResultExecutingContext filterContext)
{
//ignore anything that is not IRenderController
if ((filterContext.Controller is IRenderController) == false && filterContext.IsChildAction == false)
return;
SetViewContext(filterContext);
}
private void SetViewContext(ControllerContext controllerContext)
{
var viewCtx = new ViewContext(
filterContext.Controller.ControllerContext,
new DummyView(),
filterContext.Controller.ViewData, filterContext.Controller.TempData,
controllerContext,
new DummyView(),
controllerContext.Controller.ViewData, controllerContext.Controller.TempData,
new StringWriter());
//set the special data token
filterContext.RequestContext.RouteData.DataTokens[Constants.DataTokenCurrentViewContext] = viewCtx;
controllerContext.RequestContext.RouteData.DataTokens[Constants.DataTokenCurrentViewContext] = viewCtx;
}
private class DummyView : IView

View File

@@ -120,9 +120,13 @@ namespace Umbraco.Web.Mvc
base.InitializePage();
if (ViewContext.IsChildAction == false)
{
//always ensure the special data token is set - this is used purely for partial view macros that contain forms
// and mostly just when rendered within the RTE
ViewContext.RouteData.DataTokens[Constants.DataTokenCurrentViewContext] = ViewContext;
//this is used purely for partial view macros that contain forms
// and mostly just when rendered within the RTE - This should already be set with the
// EnsurePartialViewMacroViewContextFilterAttribute
if (ViewContext.RouteData.DataTokens.ContainsKey(Constants.DataTokenCurrentViewContext) == false)
{
ViewContext.RouteData.DataTokens.Add(Constants.DataTokenCurrentViewContext, ViewContext);
}
}
}