title: Render a list of links in my Component MVC Sitecore
body: <p>I am having some difficulties in understanding how to display links in a Sitecore EXM email component.</p>
  
  <p>I am using Sitecore MVC Controller Rendering. I am essentially trying to duplicate the Main Section component that comes as default with the Sitecore EXM platform.</p>
  
  <p>So to start with I have a controller MainSectionMVCController.cs:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.Mvc;
  using TestSitecore.Repositories;
  
  namespace TestSitecore.Controllers
  {
      public class MainSectionMVCController : Controller
      {
          // GET: MainSectionMVC
          public ActionResult Index()
          {
              var repository = new MainSectionMVCRepository();
              var mainSectionMVC = repository.GetMainSectionMVC();
  
              var mainSectionMVCViewModel = repository.GetMainSectionMVCViewModel(mainSectionMVC);
  
              return View(mainSectionMVCViewModel);
          }
      }
  }
  </code></pre>
  
  <p>I have a repository MainSectionMVCRepository.cs:</p>
  
  <pre><code>using Sitecore.Links;
  using Sitecore.Mvc.Presentation;
  using Sitecore.Web.UI.WebControls;
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using TestSitecore.Models;
  using TestSitecore.Models.MVCEmailModels;
  using Sitecore.Data.Items;
  using Sitecore.Collections;
  
  namespace TestSitecore.Repositories
  {
      public class MainSectionMVCRepository
      {
          public MainSectionMVC GetMainSectionMVC()
          {
              var mainSectionMVC = new MainSectionMVC();
              var rendering = RenderingContext.Current.Rendering;
              var dataSource = rendering.Item;
  
              mainSectionMVC.Title = new HtmlString(FieldRenderer.Render(dataSource, Constants.FIELD_TITLE));
              mainSectionMVC.Text = new HtmlString(FieldRenderer.Render(dataSource, Constants.FIELD_TEXT));
              mainSectionMVC.Image = new HtmlString(FieldRenderer.Render(dataSource, Constants.FIELD_IMAGE));
  
              mainSectionMVC.Links = GetLinks(dataSource);
  
              return mainSectionMVC;
          }
  
          private List&lt;Item&gt; GetLinks(Item dataSource)
          {            
              List&lt;Item&gt; links = new List&lt;Item&gt;();
  
              // I need to get access the the Links that are attached to the datasource item ???? How to do this
  
              return links;
          }
  
          public MainSectionMVCViewModel GetMainSectionMVCViewModel(MainSectionMVC mainSectionMVC)
          {
              var viewModel = new MainSectionMVCViewModel();
              var rendering = RenderingContext.Current.Rendering;
              //viewModel.Background = rendering.Parameters["Background"];
              viewModel.ContextItem = PageContext.Current.Item;
              viewModel.MainSectionMVC = mainSectionMVC;
  
              return viewModel;
          }
      }
  }
  </code></pre>
  
  <p>and a model MainSectionMVC.cs:</p>
  
  <pre><code>using Sitecore.Data.Items;
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  
  namespace TestSitecore.Models.MVCEmailModels
  {
      public class MainSectionMVC
      {
          public HtmlString Title { get; set; }
          public HtmlString Image { get; set; }
          public HtmlString Text { get; set; }
  
          public List&lt;Item&gt; Links { get; set; }
  
      }
  }
  </code></pre>
  
  <p>I have a ViewModel MainSectionMVCViewModel.cs:</p>
  
  <pre><code>using Sitecore.Data.Items;
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  
  namespace TestSitecore.Models.MVCEmailModels
  {
      public class MainSectionMVCViewModel
      {
          public MainSectionMVC MainSectionMVC { get; set; }
          public string Background { get; set; }
          public Item ContextItem { get; set; }
  
      }
  }
  </code></pre>
  
  <p>and finally I have a Index.cshtml that is linked to the Index action of the controller:</p>
  
  <pre><code>@using Sitecore.Mvc
  @using Sitecore.Data.Items
  @using Sitecore.Web.UI.WebControls;
  @using TestSitecore.Models
  @using TestSitecore.Models.MVCEmailModels
  
  @model MainSectionMVCViewModel
  
  &lt;div class="highlight"&gt;
      &lt;div class="row"&gt;
          &lt;div class="col-md-2 col-lg-2"&gt;&lt;/div&gt;
          &lt;div class="col-md-5 col-lg-5"&gt;
              &lt;h2&gt;@Model.MainSectionMVC.Title&lt;/h2&gt;
          &lt;/div&gt;
          &lt;div class="col-md-3 col-lg-3" style="align-content: center"&gt;
              @Model.MainSectionMVC.Image
          &lt;/div&gt;
          &lt;div class="col-md-2 col-lg-2"&gt;&lt;/div&gt;
      &lt;/div&gt;
      &lt;div class="row"&gt;
          &lt;div class="col-md-8 col-lg-8"&gt;
              &lt;h2&gt;@Model.MainSectionMVC.Text&lt;/h2&gt;
          &lt;/div&gt;
          &lt;div class="col-md-4 col-lg-4"&gt;&lt;/div&gt;
      &lt;/div&gt;
      &lt;div class="row"&gt;
          &lt;!--DISPLAY THE LINKS HERE--&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>My issue is how to populate the list of links in my Repository and then render the output on my component.</p>
  
  <p>Looking at the Template from the Link Item in Sitecore reveals that the data definition contains 2 fields, Text and Destination.</p>
  
  <p>So I assume that the Text field is used as display text for the link and the destination is actually the ItemPath/ItemID.</p>
  
  <p>I have tried using <code>var children = dataSource.GetChildren();</code> This gets me the all children of the parent Item (Main Section). Currently only Links can be child items of the Main Section Item, but this may change if the component template is modified.</p>
  
  <p>I struggled to find a suitable way to test if the child items were Links. So any tips on testing Item types would be appreciated also.</p>
  
  <p>Any Ideas?</p>