Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Block model
- ====================================================
- using System;
- using System.Collections.Generic;
- using System.ComponentModel.DataAnnotations;
- using EPiServer;
- using EPiServer.Core;
- using EPiServer.DataAbstraction;
- using EPiServer.DataAnnotations;
- using EPiServer.Web.WebControls;
- using lia_aklagare.Models.Pages;
- namespace lia_aklagare.Models.Blocks
- {
- [ContentType(DisplayName = "NewsBlock", GUID = "20293952-c702-40b3-9ee1-f4f50009f0d3", Description = "")]
- public class NewsBlock : BlockData
- {
- [CultureSpecific]
- [Required(AllowEmptyStrings = false)]
- [Display(
- GroupName = SystemTabNames.Content,
- Order = 1)]
- public virtual string Headline { get; set; }
- [CultureSpecific]
- [Display(
- GroupName = SystemTabNames.Content,
- Order = 2)]
- public virtual bool HasPagination { get; set; }
- [CultureSpecific]
- [Display(
- GroupName = SystemTabNames.Content,
- Order = 3)]
- public virtual int Quantity { get; set; }
- public virtual PageReference RootUrl { get; set; }
- public virtual PageReference ReadMoreReference { get; set; }
- }
- }
- ====================================================
- Block Controller
- ====================================================
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using Castle.Core.Internal;
- using EPiServer;
- using EPiServer.Core;
- using EPiServer.ServiceLocation;
- using EPiServer.Web;
- using EPiServer.Web.Mvc;
- using lia_aklagare.Models.Blocks;
- using lia_aklagare.Models.Pages;
- using lia_aklagare.Models.ViewModels;
- using Microsoft.Ajax.Utilities;
- namespace lia_aklagare.Controllers
- {
- public class NewsBlockController : BlockController<NewsBlock>
- {
- public override ActionResult Index(NewsBlock currentBlock)
- {
- var viewModel = new NewsBlockViewModel(currentBlock);
- var pages = ServiceLocator.Current.GetInstance<IContentRepository>()
- .GetChildren<NewsPage>(currentBlock.RootUrl)
- .ToList();
- ViewBag.TotalPages = pages.Count();
- if (currentBlock.HasPagination)
- {
- if (Request.QueryString["pg"] != null &&
- int.Parse(Request.QueryString["pg"]) > 1 &&
- int.Parse(Request.QueryString["pg"]) <= pages.Count()
- )
- {
- viewModel.NewsPages =
- pages.Skip(currentBlock.Quantity * int.Parse(Request.QueryString["pg"]) - currentBlock.Quantity).Take(currentBlock.Quantity);
- }
- else
- {
- viewModel.NewsPages = pages.Take(currentBlock.Quantity);
- }
- }
- else
- {
- viewModel.NewsPages = pages.Take(currentBlock.Quantity);
- }
- return PartialView(viewModel);
- }
- }
- }
- ====================================================
- Block View model
- ====================================================
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using EPiServer.Core;
- using lia_aklagare.Models.Blocks;
- using lia_aklagare.Models.Pages;
- namespace lia_aklagare.Models.ViewModels
- {
- public interface INewsBlockViewModel
- {
- IEnumerable<NewsPage> NewsPages { get; set; }
- }
- public sealed class NewsBlockViewModel : INewsBlockViewModel
- {
- public NewsBlockViewModel(NewsBlock currentBlock)
- {
- this.Headline = currentBlock.Headline;
- this.HasPagination = currentBlock.HasPagination;
- this.Quantity = currentBlock.Quantity;
- this.ReadMoreReference = currentBlock.ReadMoreReference;
- }
- public IEnumerable<NewsPage> NewsPages { get; set; }
- public string Headline { get; set; }
- public bool HasPagination { get; set; }
- public int Quantity { get; set; }
- public PageReference ReadMoreReference { get; set; }
- }
- }
- ====================================================
- Block View
- ====================================================
- @using EPiServer.Web.Routing
- @model lia_aklagare.Models.ViewModels.NewsBlockViewModel
- <div class="cision-news-block">
- <div class="inner-article-wrapper">
- <h3 class="c-nb-headline">
- @Model.Headline
- </h3>
- @foreach (var item in Model.NewsPages)
- {
- <div class="news-story">
- <a href="@UrlResolver.Current.GetUrl(item)">
- <div class="col-70">
- <h4>@item.Headline</h4>
- <span class="pub-date" id="pubDate">@item.Date.ToString("yyyy-MM-dd")</span>
- <p class="intro-text">@item.Preface</p>
- </div>
- <div class="col-30">
- <div class="newsblock-image">
- <img src="@item.NewsImage" />
- </div>
- </div>
- <div class="clearfix"></div>
- </a>
- </div>
- }
- @if (Model.HasPagination)
- {
- <div class="cision-news-pagination">
- <ul>
- @for (var i = 0; i < (ViewBag.TotalPages / Model.Quantity) + 1; i++)
- {
- if (ViewBag.TotalPages % Model.Quantity == 0 && i == ViewBag.TotalPages / Model.Quantity)
- {
- continue;
- }
- <li class="pageination-li">
- <a href="?pg=@(i + 1)" class="pg-a-link">@(i + 1)</a>
- </li>
- }
- </ul>
- <script type="text/javascript">
- $(document).ready(function () {
- var param = document.URL.split('?pg=')[1];
- $('.pageination-li a').each(function (index) {
- if (param === $(this).text()) {
- $(this).addClass('selected');
- }
- });
- });
- </script>
- </div>
- }
- else
- {
- <a href="@UrlResolver.Current.GetUrl(@Model.ReadMoreReference)" class="c-ng-readmore">@Html.Translate("/blocks/read-more")<span class="ion-android-arrow-dropright"></span></a>
- }
- </div>
- </div>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement