asp.net-mvc - 从控制器中的模型访问数据?
问题描述
我有一个显示最新文章列表的模型,我希望能够定义显示的文章数量。我创建了一个名为的属性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);
解决方案
如何将您的签名更改为类似的东西GetArticlePages(int maxCount)
,然后ArticlePages = GetArticlePages(currentContent.DisplayNumberOfPressArticles)
在您的Index
方法中调用它?
推荐阅读
- python - 训练Mask RCNN但总是从所有提案中得到0个正边界框?
- javascript - Javascript 异步与 setTimeout(..., 0)
- c++ - 多个基类可以有相同的虚方法吗?
- c - 理解 C 中的 strcpy 和 char 数组
- java - 即使条件不满足,Java 是否完全了解整个递归方法?
- javascript - 为什么 moment.js 没有从 Node.js 中的 API 格式化日期?
- object - ThreeJS - 对象格式的网格线框
- python - 如何在返回/收益中停止额外的重复,同时仍然保持给定键的运行总数:值对?
- python - pandas 数据框在 matplotlibm 中绘制为表格,但缺少第一列
- c - 为什么要替换数组的第一个值?