4

我的问题与这个问题有关但我认为与其改变问题,不如问一个新问题。

我现在有一个使用 _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. 结果。

4

1 回答 1

2

这永远不会像您认为的那样起作用:

var html = View((object)viewModel);

返回表示内容项的 HTML 的最简单方法是:

  • 用ThemedAttribute标记您的操作,即。[Themed(false)]
  • 返回new ShapeResult(this, viewModel)(完整视图)或new ShapePartialResult(this, viewModel)(部分视图)而不是Json(new { html = html })

将形状/视图渲染到动作内部的字符串也是可能的,但更棘手。

编辑:我假设你已经有/Views/ViewModel.cshtml文件了。就像下面提到的Bertrand Le Roy - 如果它不存在,您需要添加一个才能使用Shape.ViewModel().

于 2012-10-26T15:19:45.130 回答