Fixes: #U4-1142, Changed ctor for RenderModel so people can actually create one to pass in to their views if they want.

Changed the way that UmbracoTemplatePage works in that if you pass in an IPublishedContent model instead of a RenderModel,
it will be generated for you without a YSOD. Changed the UmbracoHelper ctor so that it can be passed a custom IPublishedContent model
so that it's context is of that model and not the one routed to. Added Umbraco.Web.Mvc to the imported namespaces for Views.
This commit is contained in:
Shannon Deminick
2012-11-05 11:27:28 +06:00
parent 329431e6df
commit 7df9921441
7 changed files with 98 additions and 24 deletions

View File

@@ -19,7 +19,8 @@
<add namespace="Umbraco.Web" />
<add namespace="Umbraco.Core" />
<add namespace="Umbraco.Core.Models" />
</namespaces>
<add namespace="Umbraco.Web.Mvc" />
</namespaces>
</pages>
</system.web.webPages.razor>

View File

@@ -1,3 +1,4 @@
using System;
using System.Globalization;
using Umbraco.Core.Models;
@@ -7,15 +8,43 @@ namespace Umbraco.Web.Models
/// Represents the model for the current rendering page in Umbraco
/// </summary>
public class RenderModel
{
{
/// <summary>
/// Constructor specifying both the IPublishedContent and the CultureInfo
/// </summary>
/// <param name="content"></param>
/// <param name="culture"></param>
public RenderModel(IPublishedContent content, CultureInfo culture)
{
if (content == null) throw new ArgumentNullException("content");
if (culture == null) throw new ArgumentNullException("culture");
Content = content;
CurrentCulture = culture;
}
/// <summary>
/// Constructor to set the IPublishedContent and the CurrentCulture is set by the UmbracoContext
/// </summary>
/// <param name="content"></param>
public RenderModel(IPublishedContent content)
{
if (content == null) throw new ArgumentNullException("content");
if (UmbracoContext.Current == null)
{
throw new InvalidOperationException("Cannot construct a RenderModel without specifying a CultureInfo when no UmbracoContext has been initialized");
}
Content = content;
CurrentCulture = UmbracoContext.Current.PublishedContentRequest.Culture;
}
/// <summary>
/// Returns the current IPublishedContent object
/// </summary>
public IPublishedContent Content { get; internal set; }
public IPublishedContent Content { get; private set; }
/// <summary>
/// Returns the current Culture assigned to the page being rendered
/// </summary>
public CultureInfo CurrentCulture { get; internal set; }
public CultureInfo CurrentCulture { get; private set; }
}
}

View File

@@ -66,11 +66,7 @@ namespace Umbraco.Web.Mvc
throw new NullReferenceException("There is not current PublishedContentRequest, it must be initialized before the RenderRouteHandler executes");
}
var renderModel = new RenderModel()
{
Content = docRequest.PublishedContent,
CurrentCulture = docRequest.Culture
};
var renderModel = new RenderModel(docRequest.PublishedContent, docRequest.Culture);
//put essential data into the data tokens, the 'umbraco' key is required to be there for the view engine
requestContext.RouteData.DataTokens.Add("umbraco", renderModel); //required for the RenderModelBinder

View File

@@ -28,10 +28,37 @@ namespace Umbraco.Web.Mvc
}
}
protected override void SetViewData(System.Web.Mvc.ViewDataDictionary viewData)
{
//Here we're going to check if the viewData's model is of IPublishedContent, this is basically just a helper for
//syntax on the front-end so we can just pass in an IPublishedContent object to partial views that inherit from
//UmbracoTemplatePage. Then we're going to manually contruct a RenderViewModel to pass back in to SetViewData
if (viewData.Model is IPublishedContent)
{
//change the model to a RenderModel and auto set the culture
viewData.Model = new RenderModel((IPublishedContent)viewData.Model, UmbracoContext.PublishedContentRequest.Culture);
}
base.SetViewData(viewData);
}
/// <summary>
/// Returns the a DynamicPublishedContent object
/// </summary>
public dynamic CurrentPage { get; private set; }
public dynamic CurrentPage { get; private set; }
private UmbracoHelper _helper;
/// <summary>
/// Gets an UmbracoHelper
/// </summary>
/// <remarks>
/// This ensures that the UmbracoHelper is constructed with the content model of this view
/// </remarks>
public override UmbracoHelper Umbraco
{
get { return _helper ?? (_helper = new UmbracoHelper(UmbracoContext, Model.Content)); }
}
}
}

View File

@@ -13,36 +13,40 @@ namespace Umbraco.Web.Mvc
{
}
protected override void InitializePage()
{
base.InitializePage();
PublishedContentRequest = (PublishedContentRequest)ViewContext.RouteData.DataTokens.GetRequiredObject("umbraco-doc-request");
UmbracoContext = (UmbracoContext)ViewContext.RouteData.DataTokens.GetRequiredObject("umbraco-context");
ApplicationContext = UmbracoContext.Application;
}
/// <summary>
/// Returns the current UmbracoContext
/// </summary>
public UmbracoContext UmbracoContext { get; private set; }
public UmbracoContext UmbracoContext
{
get { return (UmbracoContext) ViewContext.RouteData.DataTokens.GetRequiredObject("umbraco-context"); }
}
/// <summary>
/// Returns the current ApplicationContext
/// </summary>
public ApplicationContext ApplicationContext { get; private set; }
public ApplicationContext ApplicationContext
{
get { return UmbracoContext.Application; }
}
/// <summary>
/// Returns the current PublishedContentRequest
/// </summary>
internal PublishedContentRequest PublishedContentRequest { get; private set; }
internal PublishedContentRequest PublishedContentRequest
{
get { return (PublishedContentRequest)ViewContext.RouteData.DataTokens.GetRequiredObject("umbraco-doc-request"); }
}
private UmbracoHelper _helper;
/// <summary>
/// Gets an UmbracoHelper
/// </summary>
public UmbracoHelper Umbraco
/// <remarks>
/// This constructs the UmbracoHelper with the content model of the page routed to
/// </remarks>
public virtual UmbracoHelper Umbraco
{
get { return _helper ?? (_helper = new UmbracoHelper(UmbracoContext)); }
}

View File

@@ -19,6 +19,7 @@
<add namespace="Umbraco.Web" />
<add namespace="Umbraco.Core" />
<add namespace="Umbraco.Core.Models" />
<add namespace="Umbraco.Web.Mvc" />
</namespaces>
</pages>
</system.web.webPages.razor>

View File

@@ -36,6 +36,22 @@ namespace Umbraco.Web
private readonly UmbracoContext _umbracoContext;
private readonly IPublishedContent _currentPage;
/// <summary>
/// Custom constructor setting the current page to the parameter passed in
/// </summary>
/// <param name="umbracoContext"></param>
/// <param name="content"></param>
public UmbracoHelper(UmbracoContext umbracoContext, IPublishedContent content)
: this(umbracoContext)
{
if (content == null) throw new ArgumentNullException("content");
_currentPage = content;
}
/// <summary>
/// Standard constructor setting the current page to the page that has been routed to
/// </summary>
/// <param name="umbracoContext"></param>
public UmbracoHelper(UmbracoContext umbracoContext)
{
if (umbracoContext == null) throw new ArgumentNullException("umbracoContext");
@@ -43,7 +59,7 @@ namespace Umbraco.Web
_umbracoContext = umbracoContext;
if (_umbracoContext.IsFrontEndUmbracoRequest)
{
_currentPage = _umbracoContext.PublishedContentRequest.PublishedContent;
_currentPage = _umbracoContext.PublishedContentRequest.PublishedContent;
}
}