I have a model for displaying a list of latest articles, and I would like to be able to define how many articles are displayed. I have created a property called DisplayNumberOfPressArticles
and I would like to be able to access the value of this property from my controller.
Here is the model:
using Site.Helpers.Selections;
using Site.Models.Blocks.Data;
using EPiServer.DataAnnotations;
using EPiServer.Shell.ObjectEditing;
using System.ComponentModel.DataAnnotations;
namespace Site.Models.Blocks
{
[SiteContentType(GUID = "884202C2-61F1-4DF5-85A7-DC3D4E493F59")]
[SiteImageUrl]
public class LastArticlesTeaserBlock : SiteBlockData, PressOverviewData, ISpaceableData
{
[CultureSpecific]
public virtual string Heading { get; set; }
[Range(1, 1000)]
public virtual int DisplayNumberOfPressArticles { get; set; }
}
}
In my controller, I would like to take the value of DisplayNumberOfPressArticles
as a limit for the .Take() query:
using System;
using System.Collections.Generic;
using Site.Models.Blocks;
using EPiServer.Web.Mvc;
using System.Web.Mvc;
using Site.Models.Pages;
using Site.Models.ViewModels;
using EPiServer.Find;
using EPiServer.Find.Api;
using EPiServer.Find.Cms;
using EPiServer.Find.Framework;
using EPiServer.Logging;
using ILogger = EPiServer.Logging.ILogger;
using EPiServer.Globalization;
namespace Site.Controllers
{
public class LastArticlesTeaserBlockController : BlockController<LastArticlesTeaserBlock>
{
private static readonly ILogger Logger = LogManager.GetLogger();
public override ActionResult Index(LastArticlesTeaserBlock currentContent)
{
var model = new LastArticlesTeaserViewModel
{
Headline = currentContent.Heading,
ArticlePages = GetArticlePages(),
PaddingTop = currentContent.PaddingTop,
PaddingBottom = currentContent.PaddingBottom
};
return PartialView(model);
}
private List<PressDetailsPage> GetArticlePages()
{
List<PressDetailsPage> result = new List<PressDetailsPage>();
IClient findClient = SearchClient.Instance;
var search = findClient.Search<PressDetailsPage>();
search = search
.Filter(sp => sp.IsDeleted.Match(false)).PublishedInCurrentLanguage()
.Filter(sp => sp.Headline.Exists() | sp.Description.Exists())
.Filter(sp => sp.Language.Name.Match(ContentLanguage.PreferredCulture.Name))
.OrderByDescending(sp => sp.PublishDate, SortMissing.Last)
.Take(??);
try
{
var searchResult = search.GetContentResult();
result.AddRange(searchResult);
}
catch (NullReferenceException e)
{
Logger.Error(e.ToString());
}
return result;
}
}
}
Sorry for the newbie question, but everything I have tried hasn't worked so far. I thought I could access the model by using .Take(LastArticlesTeaserBlock.DisplayNumberOfPressArticles);
How about changing your signature to something like GetArticlePages(int maxCount)
and then invoking it like ArticlePages = GetArticlePages(currentContent.DisplayNumberOfPressArticles)
in your Index
method?