5

I am doing project in Sitecore MVC. For every view rendering we are giving one data source for model reference but I need to get the field values from multiple items. So can we design the view renderings with multiple data sources?

2 Answers 2

10

You could create a new template to create datasource items that bundle your "multiple items" (in separate link type fields, or with a multi-select type field - as you want). This way you are able to set one item as datasource and find all your items on that single datasource.

The benefit of keeping the datasource approach is that datasources can be personalized, unlike rendering parameters. You could also use shared fields if you want, re-use the bundles, ...

2
  • 1
    +1 for rendering personalization point. Commented Dec 24, 2016 at 5:26
  • Great approach, good job meeting personalization needs.
    – C Sigmon
    Commented Jan 2, 2017 at 20:30
3

You are able to use parameter templates and specify as much datasource as you need:

  1. Create a new template that uses “/System/Layout/Rendering Parameters/Standard Rendering Parameters” as a base template.
  2. Create the template fields (Datasource 2, Datasource 3, etc.) the same way you will do with any normal template. By using Standard Rendering Parameters as a base you wouldn't lose any of the Standard Template behavior your are used to.
  3. Attach your new template by using the “Parameters Template” field on your Sublayout.
  4. To add some data you just need to add the View rendering to the presentation details and edit its settings.
  5. Change your view code to handle multiple templates
4
  • One con of this method tho is that you would not be able to personalize the custom datasources. If you did it the way @Gatogordo suggests, you would be able to do that.
    – Richard Seal
    Commented Dec 23, 2016 at 14:00
  • Hi @Anton,I was tried.but its showing Item id only.but I want to show Fieldvalue what i am passing the fieldname.
    – Indrasena
    Commented Dec 23, 2016 at 15:27
  • My code is : string TitleCss = string.Empty, DescriptionCss = string.Empty; var rc = Sitecore.Mvc.Presentation.RenderingContext.CurrentOrNull; if (rc != null) { var parms = rc.Rendering.Parameters; TitleCss = parms["First DataSource"]; DescriptionCss = parms["DescriptionCss"]; } <h2>@TitleCss</h2>
    – Indrasena
    Commented Dec 23, 2016 at 15:37
  • It's working fine.I modified the code from "parms["First Datasource"]" to "Sitecore.Context.Database.GetItem(parms["First DataSource"]).Fields["Text"].Value".Now its displaying the field value. Thank you.
    – Indrasena
    Commented Dec 26, 2016 at 11:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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