Sitecore 7.1 MVC Rendering Helper passing variables
Asked Answered
I

1

10

I'm trying to add a view returned from a controller into my view statically. In standard MVC I would do something like the below.

@{Html.RenderAction("Product", "ProductListing", new {productId = product.ItemId});}

Here is my controller

[System.Web.Http.HttpGet]
public ActionResult Product(ID productId)
{
 var product= _productRepositorty.GetProduct(productId);

 return View("~/Views/Product/ProductDetails.cshtml", product);
}

So using the Sitecore rendering helper i have the below, where the ID is the rendering item within Sitecore pointing at the controller and action as above. However I am unable to pass through the productId as a parameter into the Product action (the productId is always null). Is this the correct way of passing a variable to another action?

@Html.Sitecore().Rendering("{AA6C2188-1897-4577-BE0A-25DD2BBA8AF1}", new { productId = product.ItemId })
Iodize answered 4/8, 2015 at 8:28 Comment(5)
What do you mean by ... unable to pass through the productId as a parameter? At first glance your code looks correct to meImpost
Im looking to pass the productId variable from the Rendering helper on my view into the Product(productId) action in my controller. Currently the productId parameter in my action is always null.Iodize
Any reason you aren't just calling Html.RenderAction() directly rather than jumping through Sitecore? I can understand trying to get separation, but there's always a dependency somewhere (whether that's the guid or the path to the view).Galumph
Using HTML.RenderAction was my fall back option. I did want to make use of Sitecore caching along with passing in a parameter into the rendering helper. Looks like I'll use the standard MVC method. Thanks :)Iodize
I thought caching would be the issue :) If you can't cache the parent rendering then @Shane's solution looks good if that works.Galumph
A
4

As far as I'm aware this syntax is not supported for passing parameters to insert into the action.

The parameters you specify are put into the rendering.Properties collection.

Would it be possible to rewrite the action signature and use the RenderingContext.Current.Rendering.Properties inside to read the value?

Adumbral answered 4/8, 2015 at 15:5 Comment(2)
Thanks, I'll see if I can access the variable via the rendering properties.Iodize
Really?! FFS sitecore. Why must you break MVC so much. Thanks for the info.Inconveniency

© 2022 - 2024 — McMap. All rights reserved.