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
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, ...
-
1
-
You are able to use parameter templates and specify as much datasource as you need:
- Create a new template that uses “/System/Layout/Rendering Parameters/Standard Rendering Parameters” as a base template.
- 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.
- Attach your new template by using the “Parameters Template” field on your Sublayout.
- To add some data you just need to add the View rendering to the presentation details and edit its settings.
- Change your view code to handle multiple templates
-
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. 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> 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. Commented Dec 26, 2016 at 11:17