我的问题与此one有关,但我认为不是changing a question而是提出新问题会更好。
我现在使用_taxonomyService.GetContentItems(term)获得了IContent项目列表
正如@Bertrand Le Roy在上述问题中所建议的那样
但是如何将其转换为有用的Html字符串,我可以通过ajax帖子在客户端上更新?
public class HomeController : Controller
{
private readonly IOrchardServices _services;
private readonly IBlogService _blogService;
private readonly IBlogPostService _blogPostService;
private readonly IFeedManager _feedManager;
private readonly IArchiveConstraint _archiveConstraint;
private readonly ITaxonomyService _taxonomyService;
public HomeController(
IOrchardServices services,
IBlogService blogService,
IBlogPostService blogPostService,
IFeedManager feedManager,
IShapeFactory shapeFactory,
IArchiveConstraint archiveConstraint,
ITaxonomyService taxonomyService) {
_services = services;
_blogService = blogService;
_blogPostService = blogPostService;
_feedManager = feedManager;
_archiveConstraint = archiveConstraint;
T = NullLocalizer.Instance;
Shape = shapeFactory;
_taxonomyService = taxonomyService;
}
dynamic Shape { get; set; }
public Localizer T { get; set; }
public ActionResult Index()
{
return View();
}
[HttpPost]
public JsonResult ListByArchive(string path, IEnumerable<string> category)
{
try
{
// get year and month from path
path = path.ToLower().Substring(path.LastIndexOf(@"/archive/", StringComparison.Ordinal) + 9);
var date = path.Split('/');
var month = int.Parse(date[1]);
var year = int.Parse(date[0]);
// get list of terms ids from strings
var taxonomyPart = _taxonomyService.GetTaxonomyByName("Category");
var terms = category.Select(cat => _taxonomyService.GetTermByName(taxonomyPart.Id, cat)).ToList();
// get list of content items by term avoiding duplicates
var posts = new List<IContent>();
foreach (var term in terms)
{
var items = _taxonomyService.GetContentItems(term);
foreach (var item in items)
{
if (!posts.Select(p => p.Id).Contains(item.Id))
{
posts.Add(item);
}
}
}
// filter by date
var byDate = posts.Where(x =>
{
var publishedUtc = x.ContentItem.As<CommonPart>().CreatedUtc;
return
publishedUtc != null
&& publishedUtc.Value.Month == month
&& publishedUtc.Value.Year == year;
});
...
这让我获得了IContent列表,但是如何获取渲染列表的html?
我试过
var range = byDate.Select(x => _services.ContentManager.BuildDisplay(x, "Summary"));
var list = Shape.List();
list.AddRange(range);
dynamic viewModel = Shape.ViewModel().ContentItems(list);
var html = View((object)viewModel);
return Json(new { html = html });
但它返回一个空视图,
{"html":{"MasterName":"","Model":[],"TempData":[],"View":null,"ViewBag":{},"ViewData":[],"ViewEngineCollection":[{"HostContainer":{}}],"ViewName":""}}
我有一个名为ListByArchive.cshtml的视图,它与orchard.blog模块匹配。
顺便说一句,我应该返回部分视图结果,而不是jason结果,但是当我更改Action结果类型时,我从服务器获得404.结果。
答案 0 :(得分:2)
这永远不会像你想象的那样发挥作用:
var html = View((object)viewModel);
返回表示内容项的HTML的最简单方法是:
[Themed(false)]
new ShapeResult(this, viewModel)
(完整视图)或new ShapePartialResult(this, viewModel)
(部分视图)而不是Json(new { html = html })
将形状/视图渲染到动作内部的字符串也是可能的,但是更加棘手。
编辑:我认为您已经有/Views/ViewModel.cshtml
个文件。与下面提到的Bertrand Le Roy一样 - 如果不存在,则需要添加一个才能使用Shape.ViewModel()
创建形状。