0
votes

Using Sitecore 8.0, MVC, VS2015
I managed to generate the footer content as desired, for a practice site, but would like to know the standard way of doing things. The question is simple, but tried to explain in detail. Please bear with that :)

Requirement: The footer should display Office addresses and they should be editable by the Content author. enter image description here Sitecore Template : Location, Telephone 1, Telephone 2, IsActive(type-checkbox).
Datasource : A folder with items of the above template.
Code:

    public class FooterViewModel 
        {
            public List<Sitecore.Data.Items.Item> Addresses { get; set; }
        }  

    public class FooterController : Controller
        {
            public ActionResult Footer()
            {
                var datasource = RenderingContext.Current.Rendering.Item;
                FooterViewModel viewModel = new FooterViewModel();            
                viewModel.Addresses = new List<Item>();

            if(datasource != null && datasource.HasChildren && datasource.Children.Count > 0)
                {
                    foreach(Item address in datasource.Children)
                    {
                        if (address["IsActive"] == "1")
                            viewModel.Addresses.Add(address);
                    }
                }

                return View("~/Views/Shared/Footer.cshtml", viewModel);            
            }
        }

Rendering the html using a Sitecore Controller Rendering
cshtml:

@using Sitecore.Mvc
@using Sitecore.Mvc.Presentation
@model Democore.Models.FooterViewModel

<div>
    @foreach (var address in Model.Addresses)
    {
        <div>
            <h3>@Html.Sitecore().Field("Location", address)</h3>
            <ul>         
                <li>
                    @Html.Sitecore().Field("Telephone 1", address) 
                </li>
                <li>
                    @Html.Sitecore().Field("Telephone 2", address) 
                </li>
            </ul>
        </div>
    }  
</div>    
<div>
    <p>&copy Copyright @DateTime.Now.Year. All rights reserved</p>
</div>

Here are my questions. (..well all 3 are more or less similar)

  1. How to better this code/structure (or) in which case might it fail.

  2. I did not like the way I hardcoded the field names in controller & cshtml. What if the author changes the field names. How to tackle that.

  3. How does it work in a real scenario, say for eg if author wants to show a third phone number. Will they contact the developer? Because that requires a change in design & code too right?
1
As this will likely be closed do to the answer being broad and primarily opinion based - please give your support to this: area51.stackexchange.com/proposals/101710/sitecore - it will be the perfect place for questions like this.Richard Seal

1 Answers

1
votes

This is a very broad question and will probably get closed -but here are some tips!

  1. You controller is fine as far as error handling goes. You really want to add a global error handler for your site. This is a good example of that: http://www.sitecorenutsbolts.net/2015/10/23/Rendering-Exception-Handling-The-Right-Way/

  2. You have 2 options - use constants and field ID's not the names, not a great option but works. A better option would be to use an ORM/Wrapper to do that. Two good ones are Fortis and Glass Mapper - both are very good - I contribute to Fortis so that would be my recomendation.

  3. Normally if the design of the component changes it will require development support. You could use something like BrainJock's Score or ZenGarden to build your site and then the editor has a lot more control. But still likely would need a developer.

Hope this helps. For some info on good Sitecore architecture look here: Sitecore Helix and Sitecore Habitat