Advertisement
Guest User

Untitled

a guest
Sep 14th, 2015
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.61 KB | None | 0 0
  1. Block model
  2. ====================================================
  3.  
  4. using System;
  5. using System.Collections.Generic;
  6. using System.ComponentModel.DataAnnotations;
  7. using EPiServer;
  8. using EPiServer.Core;
  9. using EPiServer.DataAbstraction;
  10. using EPiServer.DataAnnotations;
  11. using EPiServer.Web.WebControls;
  12. using lia_aklagare.Models.Pages;
  13.  
  14. namespace lia_aklagare.Models.Blocks
  15. {
  16.     [ContentType(DisplayName = "NewsBlock", GUID = "20293952-c702-40b3-9ee1-f4f50009f0d3", Description = "")]
  17.     public class NewsBlock : BlockData
  18.     {
  19.         [CultureSpecific]
  20.         [Required(AllowEmptyStrings = false)]
  21.         [Display(
  22.         GroupName = SystemTabNames.Content,
  23.         Order = 1)]
  24.         public virtual string Headline { get; set; }
  25.  
  26.         [CultureSpecific]
  27.         [Display(
  28.         GroupName = SystemTabNames.Content,
  29.         Order = 2)]
  30.         public virtual bool HasPagination { get; set; }
  31.  
  32.         [CultureSpecific]
  33.         [Display(
  34.         GroupName = SystemTabNames.Content,
  35.         Order = 3)]
  36.         public virtual int Quantity { get; set; }
  37.         public virtual PageReference RootUrl { get; set; }
  38.         public virtual PageReference ReadMoreReference { get; set; }
  39.     }
  40. }
  41.  
  42. ====================================================
  43. Block Controller
  44. ====================================================
  45. using System;
  46. using System.Collections.Generic;
  47. using System.Linq;
  48. using System.Web;
  49. using System.Web.Mvc;
  50. using Castle.Core.Internal;
  51. using EPiServer;
  52. using EPiServer.Core;
  53. using EPiServer.ServiceLocation;
  54. using EPiServer.Web;
  55. using EPiServer.Web.Mvc;
  56. using lia_aklagare.Models.Blocks;
  57. using lia_aklagare.Models.Pages;
  58. using lia_aklagare.Models.ViewModels;
  59. using Microsoft.Ajax.Utilities;
  60.  
  61. namespace lia_aklagare.Controllers
  62. {
  63.     public class NewsBlockController : BlockController<NewsBlock>
  64.     {
  65.         public override ActionResult Index(NewsBlock currentBlock)
  66.         {
  67.             var viewModel = new NewsBlockViewModel(currentBlock);
  68.             var pages = ServiceLocator.Current.GetInstance<IContentRepository>()
  69.                 .GetChildren<NewsPage>(currentBlock.RootUrl)
  70.                 .ToList();
  71.             ViewBag.TotalPages = pages.Count();
  72.  
  73.             if (currentBlock.HasPagination)
  74.             {
  75.                 if (Request.QueryString["pg"] != null &&
  76.                     int.Parse(Request.QueryString["pg"]) > 1 &&
  77.                     int.Parse(Request.QueryString["pg"]) <= pages.Count()
  78.                    )
  79.                 {
  80.                     viewModel.NewsPages =
  81.                         pages.Skip(currentBlock.Quantity * int.Parse(Request.QueryString["pg"]) - currentBlock.Quantity).Take(currentBlock.Quantity);
  82.                 }
  83.                 else
  84.                 {
  85.                     viewModel.NewsPages = pages.Take(currentBlock.Quantity);
  86.                 }
  87.                
  88.             }
  89.             else
  90.             {
  91.                 viewModel.NewsPages = pages.Take(currentBlock.Quantity);
  92.             }
  93.  
  94.             return PartialView(viewModel);
  95.         }
  96.     }
  97. }
  98.  
  99. ====================================================
  100. Block View model
  101. ====================================================
  102.  
  103. using System;
  104. using System.Collections.Generic;
  105. using System.Linq;
  106. using System.Web;
  107. using EPiServer.Core;
  108. using lia_aklagare.Models.Blocks;
  109. using lia_aklagare.Models.Pages;
  110.  
  111. namespace lia_aklagare.Models.ViewModels
  112. {
  113.     public interface INewsBlockViewModel
  114.     {
  115.         IEnumerable<NewsPage> NewsPages { get; set; }
  116.     }
  117.  
  118.     public sealed class NewsBlockViewModel : INewsBlockViewModel
  119.     {
  120.         public NewsBlockViewModel(NewsBlock currentBlock)
  121.         {
  122.             this.Headline = currentBlock.Headline;
  123.             this.HasPagination = currentBlock.HasPagination;
  124.             this.Quantity = currentBlock.Quantity;
  125.             this.ReadMoreReference = currentBlock.ReadMoreReference;
  126.         }
  127.         public IEnumerable<NewsPage> NewsPages { get; set; }
  128.         public string Headline { get; set; }
  129.         public bool HasPagination { get; set; }
  130.         public int Quantity { get; set; }
  131.         public PageReference ReadMoreReference { get; set; }
  132.     }
  133. }
  134.  
  135. ====================================================
  136. Block View
  137. ====================================================
  138. @using EPiServer.Web.Routing
  139. @model lia_aklagare.Models.ViewModels.NewsBlockViewModel
  140.  
  141. <div class="cision-news-block">
  142.     <div class="inner-article-wrapper">
  143.         <h3 class="c-nb-headline">
  144.             @Model.Headline
  145.         </h3>
  146.         @foreach (var item in Model.NewsPages)
  147.             {
  148.             <div class="news-story">
  149.                 <a href="@UrlResolver.Current.GetUrl(item)">
  150.                     <div class="col-70">
  151.                         <h4>@item.Headline</h4>
  152.                         <span class="pub-date" id="pubDate">@item.Date.ToString("yyyy-MM-dd")</span>
  153.                         <p class="intro-text">@item.Preface</p>
  154.                     </div>
  155.                     <div class="col-30">
  156.                         <div class="newsblock-image">
  157.                             <img src="@item.NewsImage" />
  158.                         </div>
  159.                     </div>
  160.                     <div class="clearfix"></div>
  161.                 </a>
  162.             </div>
  163.         }
  164.  
  165.         @if (Model.HasPagination)
  166.             {
  167.             <div class="cision-news-pagination">
  168.                 <ul>
  169.                     @for (var i = 0; i < (ViewBag.TotalPages / Model.Quantity) + 1; i++)
  170.                 {
  171.                     if (ViewBag.TotalPages % Model.Quantity == 0 && i == ViewBag.TotalPages / Model.Quantity)
  172.                     {
  173.                         continue;
  174.                     }
  175.                     <li class="pageination-li">
  176.                         <a href="?pg=@(i + 1)" class="pg-a-link">@(i + 1)</a>
  177.                     </li>
  178.                     }
  179.                 </ul>
  180.                 <script type="text/javascript">
  181.                     $(document).ready(function () {
  182.                         var param = document.URL.split('?pg=')[1];
  183.  
  184.                         $('.pageination-li a').each(function (index) {
  185.                             if (param === $(this).text()) {
  186.                                 $(this).addClass('selected');
  187.                             }
  188.                         });
  189.                     });
  190.                 </script>
  191.             </div>
  192.         }
  193.         else
  194.         {
  195.             <a href="@UrlResolver.Current.GetUrl(@Model.ReadMoreReference)" class="c-ng-readmore">@Html.Translate("/blocks/read-more")<span class="ion-android-arrow-dropright"></span></a>
  196.         }
  197.     </div>
  198. </div>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement