【发布时间】:2013-06-19 08:24:37
【问题描述】:
我只是想知道如何在我的控制器中获取特定的 ContentItem。
我想获取特定的内容项目,然后在我的自定义视图上显示它的形状.. PS:如果我使用 ContentManager.Get(ID),我也不知道如何获取内容项的 ID
[HttpGet]
public ActionResult Index(string jobType, string location) {
var vm = new SearchForJobViewModel();
var items = new List<CustomPart>();
// Load the WhatsAround content items
IEnumerable<ContentItem> whatsAroundContentItems = ContentManager.Query().ForType("Custom").List();
foreach (ContentItem contentItem in whatsAroundContentItems)
{
ContentItemRecord contentItemRecord = contentItem.Record;
if (contentItem == null)
continue;
//CustomPart item = new CustomPart(contentItemRecord.Data);
//items.Add(item);
}
//Im also planning to pass the ContentItem in the view and then render it there
//return View("../JobSearchResults", items.Single(i => i.Name == "MyContentItem");
return View("../JobSearchResults", vm);
}
附加
[Themed]
[HttpGet]
public ActionResult Index(string jobType, string location) {
var vm = new SearchForJobViewModel();
vm.SelectedJobType = jobType;
vm.SelectedLocation = location;
//query all the content items
IEnumerable<ContentItem> items = ContentManager.Query().List();
foreach (ContentItem contentItem in items) {
ContentItemRecord contentItemRecord = contentItem.Record;
if (contentItem == null)
continue;
if (contentItemRecord.ContentType.Name == "CustomPage") {
// I just painfully search the contents just to get the ID of the specific content item that I want to display
// I dont know what table where I can see the ID on the content items
if (contentItemRecord.Id == 40) {
ContentItem ci = contentItem;
var test = ci.As<CustomPart>().Scripts; //custom part that I made
// the body part (raw html from the wysiwg editor)
// this I wil render it in my view
vm.Body = ci.As<BodyPart>().Text;
}
}
}
return View("../JobSearchResults", vm);
}
JobSearchResults.cshtml
@Html.Raw(Model.Body)
它实际上看起来很奇怪,但我正在尝试使用 Orchard CMS 而不使用 Orchard 核心(内容部分、驱动程序、处理程序等)
【问题讨论】:
-
真的不清楚你想做什么,你尝试了什么以及它是如何失败的。
-
好吧,我只是想获取一个特定的内容项并将其显示在我的视图中。请看我的编辑..
-
在哪个特定表中我可以获得内容项的 ID,我可以在 contextManager.Get(ID) 中使用
-
问题是,你的代码效率极低:你得到所有个内容项,然后你循环它们来找到你想要的。相反,学习如何使用查询 API,这样您就不必执行那个可怕的循环。查找 ID 是您最少的问题,查看此代码,但每个部分都有一个 ID 属性,该属性也是内容项的 ID。此外,您甚至不需要通过 ID 获取内容项,就好像您引用了某个部分一样,您已经可以访问该内容项及其任何部分。