Fixes: #U4-1844, #U4-1730, #U4-1843

This commit is contained in:
Shannon Deminick
2013-03-06 04:12:28 +06:00
parent 9b6d498cf7
commit d48e339b81
4 changed files with 85 additions and 11 deletions

View File

@@ -30,7 +30,11 @@ namespace Umbraco.Web.Macros
[ChildActionOnly]
public PartialViewResult Index()
{
var model = new PartialViewMacroModel(_currentPage.ConvertFromNode(),
var model = new PartialViewMacroModel(
_currentPage.ConvertFromNode(),
_macro.Id,
_macro.Alias,
_macro.Name,
_macro.Properties.ToDictionary(x => x.Key, x => (object)x.Value));
return PartialView(_macro.ScriptName, model);
}

View File

@@ -1,4 +1,5 @@
using System.Web.Mvc;
using Umbraco.Core.Models;
using Umbraco.Web.Models;
using Umbraco.Web.Mvc;
@@ -9,6 +10,19 @@ namespace Umbraco.Web.Macros
/// </summary>
public abstract class PartialViewMacroPage : UmbracoViewPage<PartialViewMacroModel>
{
protected override void InitializePage()
{
base.InitializePage();
//set the model to the current node if it is not set, this is generally not the case
if (Model != null)
{
CurrentPage = Model.Content.AsDynamic();
}
}
/// <summary>
/// Returns the a DynamicPublishedContent object
/// </summary>
public dynamic CurrentPage { get; private set; }
}
}

View File

@@ -1,4 +1,6 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using Umbraco.Core.Models;
namespace Umbraco.Web.Models
@@ -9,14 +11,38 @@ namespace Umbraco.Web.Models
public class PartialViewMacroModel
{
public PartialViewMacroModel(IPublishedContent page,
int macroId,
string macroAlias,
string macroName,
IDictionary<string, object> macroParams)
{
Content = page;
MacroParameters = macroParams;
MacroName = macroName;
MacroAlias = macroAlias;
MacroId = macroId;
}
[EditorBrowsable(EditorBrowsableState.Never)]
[Obsolete("Use the constructor accepting the macro id instead")]
public PartialViewMacroModel(IPublishedContent page, IDictionary<string, object> macroParams)
{
CurrentPage = page;
Content = page;
MacroParameters = macroParams;
}
public IPublishedContent CurrentPage { get; private set; }
[EditorBrowsable(EditorBrowsableState.Never)]
[Obsolete("Use the Content property instead")]
public IPublishedContent CurrentPage
{
get { return Content; }
}
public IPublishedContent Content { get; private set; }
public string MacroName { get; private set; }
public string MacroAlias { get; private set; }
public int MacroId { get; private set; }
public IDictionary<string, object> MacroParameters { get; private set; }
}

View File

@@ -77,20 +77,50 @@ namespace Umbraco.Web.Mvc
{
get
{
var routeData = ControllerContext.IsChildAction
? ControllerContext.ParentActionViewContext.RouteData
: ControllerContext.RouteData;
if (!routeData.DataTokens.ContainsKey("umbraco-route-def"))
var routeDefAttempt = TryGetRouteDefinitionFromAncestorViewContexts();
if (!routeDefAttempt.Success)
{
throw new InvalidOperationException("Cannot find the Umbraco route definition in the route values, the request must be made in the context of an Umbraco request");
throw routeDefAttempt.Error;
}
var routeDef = (RouteDefinition)routeData.DataTokens["umbraco-route-def"];
var routeDef = routeDefAttempt.Result;
return routeDef.PublishedContentRequest.PublishedContent;
}
}
/// <summary>
/// we need to recursively find the route definition based on the parent view context
/// </summary>
/// <returns></returns>
/// <remarks>
/// We may have Child Actions within Child actions so we need to recursively look this up.
/// see: http://issues.umbraco.org/issue/U4-1844
/// </remarks>
private Attempt<RouteDefinition> TryGetRouteDefinitionFromAncestorViewContexts()
{
ControllerContext currentContext = ControllerContext;
while (currentContext != null)
{
var currentRouteData = currentContext.RouteData;
if (currentRouteData.DataTokens.ContainsKey("umbraco-route-def"))
{
return new Attempt<RouteDefinition>(true, (RouteDefinition) currentRouteData.DataTokens["umbraco-route-def"]);
}
if (currentContext.IsChildAction)
{
//assign current context to parent
currentContext = currentContext.ParentActionViewContext;
}
else
{
//exit the loop
currentRouteData = null;
}
}
return new Attempt<RouteDefinition>(
new InvalidOperationException("Cannot find the Umbraco route definition in the route values, the request must be made in the context of an Umbraco request"));
}
}
}