Sitecore Treelist及Source Image选中值在CSHTML中显示失败求助
Got it, let's work through this together. I’ve spent plenty of time debugging Sitecore field rendering in CSHTML, so let’s break down how to get your Treelist selected items and associated source images displaying properly, plus fix why your current code might be failing.
Rendering Selected Treelist Items in CSHTML
First, let's start with a reliable implementation to pull and display your Treelist's selected items. The key here is safely accessing the field value, splitting it into item IDs, and fetching each corresponding Sitecore item.
@using Sitecore.Mvc @using Sitecore.Data.Items @using Sitecore.Data @{ // Grab the current Sitecore item (adjust this if you're targeting a specific item instead) var currentItem = Sitecore.Context.Item; if (currentItem == null) { <p>No item found to render.</p> return; } // Replace "YourTreelistFieldName" with the exact field name/key from your Sitecore template var treelistField = currentItem.Fields["YourTreelistFieldName"]; if (treelistField == null || string.IsNullOrWhiteSpace(treelistField.Value)) { <p>No items selected in the Treelist.</p> return; } } <h3>Selected Treelist Items:</h3> <ul> @foreach (var itemIdString in treelistField.Value.Split('|', StringSplitOptions.RemoveEmptyEntries)) { if (ID.TryParse(itemIdString, out ID itemId)) { var selectedItem = Sitecore.Context.Database.GetItem(itemId); if (selectedItem != null) { <li> <strong>@selectedItem.DisplayName</strong> @* Render additional fields from the selected item here *@ @if (!string.IsNullOrWhiteSpace(selectedItem.Fields["ShortDescription"]?.Value)) { <p>@selectedItem.Fields["ShortDescription"].Value</p> } </li> } else { <li class="text-danger">Item with ID @itemIdString not found in the database.</li> } } else { <li class="text-danger">Invalid item ID found in Treelist: @itemIdString</li> } } </ul>
Rendering the Associated Source Image
Assuming your Treelist selected items have an Image field (let's call it SourceImage), here's how to pull and display that image within the Treelist loop. If you're referring to a standalone Image field linked to the Treelist, the logic is similar but targeted to the current item instead.
Add this inside the foreach loop where you render each selected item:
@* Render the Source Image for the selected item *@ var imageField = (Sitecore.Data.Fields.ImageField)selectedItem.Fields["SourceImage"]; if (imageField != null && imageField.MediaItem != null) { // Generate the media URL (add parameters like width/height if needed) var mediaUrl = Sitecore.Resources.Media.MediaManager.GetMediaUrl(imageField.MediaItem, new Sitecore.Resources.Media.MediaUrlOptions { Width = 300, Height = 200, AllowStretch = true }); <img src="@mediaUrl" alt="@(string.IsNullOrWhiteSpace(imageField.Alt) ? selectedItem.DisplayName : imageField.Alt)" class="img-fluid" /> } else { <p>No source image available for @selectedItem.DisplayName.</p> }
Troubleshooting Why Your Code Isn't Working
If your current implementation is failing, here are the most common issues to check:
- Incorrect Field Names: Sitecore field names/keys are case-sensitive. Double-check the field name in your template (use the Raw Values view in the Content Editor to confirm the exact value stored in the Treelist field).
- Null Reference Exceptions: Always guard against nulls—if you try to access a field on a null item or a null field, Sitecore will throw an error. The code above includes checks for this, but your current code might be missing them.
- Database Context: If you're testing in a non-default database (e.g.,
webvsmaster), ensure you're fetching items from the correct database. UseSitecore.Configuration.Factory.GetDatabase("web")explicitly if needed. - Media Permissions: Make sure the current user has read access to the Media Items linked in the Image field. If the user can't access the media, the URL will fail.
- Treelist Value Format: Sometimes Treelist fields can have empty entries or invalid IDs. Using
StringSplitOptions.RemoveEmptyEntriesandID.TryParse(like in the code above) helps filter out bad data.
内容的提问来源于stack exchange,提问作者SSAT

