2

I'm trying to use the same control (MVC ControllerRendering) multiple times in a page type template, similar to www.launchsitecore.net/.../binding-content-and-presentation (please check Fixed Toolbar that has been used twice).

I've created a template and assigned multiple controls based on same ControllerRendering in the presentations details of template, these are using same placeholder. When I create a page based on this template I want to setup different values for the Controllers in the Content Tab of the page. At the moment it only only allows setting up content for a single control and just shows same content on all the instances of Controls that I've added.

The Controller code is as follows:

namespace Company.Website.Controllers
{
    public class SharedController : SitecoreController
    {
        // GET: Shared
        public ViewResult EmailForm()
        {            
            ISitecoreContext service = new SitecoreContext();

            Foreign_Language_Email_Block model = service.GetCurrentItem<Foreign_Language_Email_Block>();

            return View(model);
        }
    }
}

It uses GlassMapper to get the data from the page and the View displays the values using Razor.

Not sure what I'm missing?

Please could you all Sitecore Experts help!!

Thanks

  • 2
    Post your Controller code, please. Essentially what you are missing, is assigning a Datasource to each instance of your component on the page. And then you need to make sure the Controller picks up the assigned Datasource correctly. – Mark Cassidy May 23 '17 at 12:15
  • Mark, I've updated the description with code, but all code is not showing in the code section, just check code between the horizontal rulers. – maverick May 23 '17 at 13:36
  • I'm just using GlassMapper to get the Model and the model actually is used in the Views to display text, but as there is no option to assign text to both controllers. – maverick May 23 '17 at 13:37
2

I am presuming Foreign_Language_Email_Block is currently a base template of your page item.

If your pages will feature multiple controls of the same type, that pattern can't work. You need to use separate, dynamically bound data sources. Not only will this allow you to insert multiple distinct controls of the same type on the page, but it's also a best practice for leveraging Sitecore's advanced personalization and marketing experiment features.

What you will need to do:

  1. Create two data items based directly on your Foreign_Language_Email_Block template (or a derivative of that template). These would normally reside either within a global "resources" folder for the site (outside of the Home node), or under your page within a "page data" folder. (Names used are just examples. See this answer for more details on data source locations.)
  2. Make sure your controller inherits from GlassController, and change the logic to the following:

    public class SharedController : GlassController
    {
        public ViewResult EmailForm()
        {            
            Foreign_Language_Email_Block model = this.GetLayoutItem<Foreign_Language_Email_Block>();
            return View(model);
        }
    }
    
  3. Using Content Editor, attach the data sources to your instantiated renderings (controls). enter image description here

  4. Remove Foreign_Language_Email_Block from your page template's base templates.

Note that by calling GetLayoutItem, you can still add data directly to your page as a fallback option. This could still be a useful shortcut if you will only have one rendering of that type per page.

-1

You can take a look into Dynamic Placeholder concept in Sitecore.

If you need to add two or more components of the same type on a single page, you can add the component to the existing Placeholder and can also define the name for a new placeholder on it. The second time you want to add the component again just add it to the new placeholder defined just above.

You can think of any similar approach based on your problem complexity.

Hope it helps :)

  • This does not answer the OP's question - he needs to use Datasources. He's just putting a rendering multiple times into a placeholder. – Richard Seal May 24 '17 at 15:10
  • @RichardSeal - i tried to answer the same - even if you need to add a rendering multiple time with different datasources, you can do so. Just make sure not to add them on the same placeholder. You can add your first rendering on the placeholder declared, but to add the second rendering - add it on the dynamic placeholder which should be declared on the first rendering assigned (in the parameters section). Same rule should be followed for the other rendering as well (i.e assign them on dynamic placeholders declared on the previous rendering). – Amit Gaurav Jun 22 '17 at 8:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.