UmbracoVirtualNodeRouteHandler + TypedModel + MacroPartials Parameter from route
I guys, I am trying to supply a model or some other from of parameter from a controller / view to a macro partial and I don't now how to accomplish this. Or if it's even possible.
Some background: I work at a retailer and am building a proof-of-concept in Umbraco to replace our current webshop. Currently we have 26000 products in our database and I don't want to manage all these products in the Umbraco content
tree. We have our own Product Information Management system for that, so parameters don't come only from the macro parameters, but let's say the product number comes from the custom route, which binds to a custom controller. I want
to make a template page in the content tree which I map to a custom route with a UmbracoVirtualNodeRouteHandler. On this template page I want to place macros which can be moved and edited by our content department. In other words I want
our content department to be able to pick components I design (macros) and place them on a product detail template page so they freely can place components (macros) on the page as they want to.
As I illustrated in the previous paragraph I can't supply parameters from my custom controller to the macros to populate the macro with the correct data to display because depending on the custom route each request to the page can be a
different product. E.g: the macro for the product picture needs to know which product picture to show.
My code drilldown:
In a custom ApplicationEventHandler I map my custom routes and I use Ditto to be able to use typed models.
public class UmbracoEventHandler : ApplicationEventHandler
{
/// <summary>
/// Overridable method to execute when Bootup is completed, this allows you to perform any other bootup logic required for the application.
/// Resolution is frozen so now they can be used to resolve instances.
/// </summary>
/// <param name="umbracoApplication"></param>
/// <param name="applicationContext"></param>
protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
base.ApplicationStarted(umbracoApplication, applicationContext);
RouteConfig.RegisterRoutes(RouteTable.Routes);
}
/// <summary>
/// Overridable method to execute when All resolvers have been initialized but resolution is not frozen so they can be modified in this method
/// </summary>
/// <param name="umbracoApplication"></param>
/// <param name="applicationContext"></param>
protected override void ApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
base.ApplicationStarting(umbracoApplication, applicationContext);
var types = PluginManager.Current.ResolveTypes<PublishedContentModel>();
var factory = new DittoPublishedContentModelFactory(types);
PublishedContentModelFactoryResolver.Current.SetFactory(factory);
}
}
The custom route I register for handling the 'product detail page' request.
public class RouteConfig
{
/// <summary>
/// Registers the routes.
/// </summary>
/// <param name="routes">The routes.</param>
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapUmbracoRoute(
"products",
"product/{name}/{itemnumber}",
new
{
controller = "ProductDetail",
action = "ProductDetail",
sku = UrlParameter.Optional
},
new ProductDetailRouteHandler());
}
}
I use a UmbracoVirtualNodeRouteHandler to map the request, mapped on the custom route to a content node:
Then in the ProductDetailController I handle the request:
public class ProductDetailController : BaseRenderMvcController
{
public ActionResult ProductDetail(RenderModel model, string name, string itemnumber)
{
if (!(model.Content is ProductDetailModel)) // Ditto specific.
{
var productDetailModel = model.Content.As<ProductDetailModel>();
// Retrieve the product detail data from the PIM with the supplied 'itemnumber' parameter.
productDetailModel.SiteDescription = "Apple iPad mini 3 Wi-Fi - 64GB - MyCustomWebShop.com"; // Dummy data.
productDetailModel.SiteTitle = "Apple iPad mini 3 Wi-Fi - 64GB - MyCustomWebShop.com";
productDetailModel.ProductNumber = itemnumber;
model = new RenderModel(productDetailModel, model.CurrentCulture);
}
return View(model);
}
}
The product detail view (ProductDetail.cshtml):
@inherits Umbraco.Web.Mvc.UmbracoViewPage
@Model.GetGridHtml("content") @Render the macros (custom components)@
The macro partial (\Views\MacroPartials\ProductDetail\ProductImage.cshtml):
@using Our.Umbraco.Ditto;
@using System.Web.Mvc.Html
@using System.Linq;
@inherits Umbraco.Web.Macros.PartialViewMacroPage
@Model.MacroName
@{
var content = Model.Content;
var test1 = Umbraco.AssignedContentItem.As
// Retrieve custom image dimensions from the macro parameters as defined on the template in the content tree.
//Model.MacroParameters
// Here I want to retrieve the model I supplied in the ProductDetailController with the property productDetailModel.ProductNumber = itemnumber; filled.
// All I am able to retrieve is the data from the DB as above, and the ProductNumber property is empty.
// I tried to retrive the product number from the ViewContext.RouteData but the route to the product detail is gone, and I am only able to retrieve data used to map to the macro partial.
}
@Html.Action("GetProductImage", "ProductImage")
The Surface ProductImageController:
public class ProductImageController : SurfaceController
{
[ChildActionOnly]
public ActionResult GetProductImage()
{
return PartialView("ProductImage", model with data); // Here I am not able to retrieve data from the orignal request to fill a custom model to supply the Product image view with data.
}
}
The \Views\Partials\ProductImage.cshtml:
@inherits Umbraco.Web.Mvc.UmbracoViewPage
Product image to be rendered using the original 'itemnumber' parameter.
UmbracoVirtualNodeRouteHandler + TypedModel + MacroPartials Parameter from route
I guys, I am trying to supply a model or some other from of parameter from a controller / view to a macro partial and I don't now how to accomplish this. Or if it's even possible.
Some background: I work at a retailer and am building a proof-of-concept in Umbraco to replace our current webshop. Currently we have 26000 products in our database and I don't want to manage all these products in the Umbraco content
tree. We have our own Product Information Management system for that, so parameters don't come only from the macro parameters, but let's say the product number comes from the custom route, which binds to a custom controller. I want
to make a template page in the content tree which I map to a custom route with a UmbracoVirtualNodeRouteHandler. On this template page I want to place macros which can be moved and edited by our content department. In other words I want
our content department to be able to pick components I design (macros) and place them on a product detail template page so they freely can place components (macros) on the page as they want to.
As I illustrated in the previous paragraph I can't supply parameters from my custom controller to the macros to populate the macro with the correct data to display because depending on the custom route each request to the page can be a
different product. E.g: the macro for the product picture needs to know which product picture to show.
My code drilldown:
In a custom ApplicationEventHandler I map my custom routes and I use Ditto to be able to use typed models.
The custom route I register for handling the 'product detail page' request.
I use a UmbracoVirtualNodeRouteHandler to map the request, mapped on the custom route to a content node:
Then in the ProductDetailController I handle the request:
The product detail view (ProductDetail.cshtml):
@inherits Umbraco.Web.Mvc.UmbracoViewPage
@Model.GetGridHtml("content") @Render the macros (custom components)@
The macro partial (\Views\MacroPartials\ProductDetail\ProductImage.cshtml):
@using Our.Umbraco.Ditto; @using System.Web.Mvc.Html @using System.Linq; @inherits Umbraco.Web.Macros.PartialViewMacroPage @Model.MacroName @{ var content = Model.Content; var test1 = Umbraco.AssignedContentItem.As
}
@Html.Action("GetProductImage", "ProductImage")
The Surface ProductImageController:
The \Views\Partials\ProductImage.cshtml:
@inherits Umbraco.Web.Mvc.UmbracoViewPage
Product image to be rendered using the original 'itemnumber' parameter.
is working on a reply...