首页 > 解决方案 > 从控制器中的模型访问数据?

问题描述

我有一个显示最新文章列表的模型,我希望能够定义显示的文章数量。我创建了一个名为的属性DisplayNumberOfPressArticles,我希望能够从我的控制器访问该属性的值。

这是模型:

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; }

    }
}

在我的控制器中,我想将DisplayNumberOfPressArticles.Take() 查询的值作为限​​制:

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;

        }
    }
}

对不起新手的问题,但到目前为止我所尝试的一切都没有奏效。我以为我可以通过使用访问模型.Take(LastArticlesTeaserBlock.DisplayNumberOfPressArticles);

标签: asp.net-mvcepiserver

解决方案


如何将您的签名更改为类似的东西GetArticlePages(int maxCount),然后ArticlePages = GetArticlePages(currentContent.DisplayNumberOfPressArticles)在您的Index方法中调用它?


推荐阅读