【问题标题】:Template can't receive IList model模板无法接收 IList 模型
【发布时间】:2019-01-24 17:02:48
【问题描述】:

我正在编写一个 Umbraco 应用程序,我需要将一些模型发送到从 MasterPage 继承的页面,如下所示:

@using ContentModels = App.Models.BlogPostViewModel
@inheritsUmbraco.Web.Mvc.UmbracoTemplatePage<IList<App.Models.BlogPostViewModel>>
@{
    Layout = "Master.cshtml";
}

但我得到了错误:

“System.Collections.Generic.IList”类型必须可转换为“Umbraco.Core.Models.IPublishedContent”

这里是控制器:

public ActionResult GetBlogsByMonthId(int monthId)
{
    var rootNode = Umbraco.TypedContentAtRoot().First();
    var blogs = BlogService.GetBlogsByMonthId(rootNode, monthId);
    return View("~/Views/BlogArchivePage.cshtml", blogs);
}

怎么了?我应该创建一个新页面吗?

【问题讨论】:

    标签: razor umbraco umbraco7


    【解决方案1】:

    “IList”必须派生自 IPublishedContent 才能与 UmbracoTemplatePage 一起使用。相反,您可以使用 UmbracoViewPage,它应该可以工作 - 请参阅下面的代码 :)

     @using ContentModels = App.Models.BlogPostViewModel
     @inheritsUmbraco.Web.Mvc.UmbracoViewPage<IList<App.Models.BlogPostViewModel>>
     @{
         Layout = "Master.cshtml";
      }
    

    【讨论】:

    • 谢谢,但我收到了错误 Cannot bind source type System.Collections.Generic.List [...] to model type Umbraco.Web.Models.RenderModel. 我错过了什么吗?
    • 尝试“@model”而不是“@inherits”:)
    【解决方案2】:

    您的 BlogService.GetBlogsByMonthId(rootNode, monthId) 是否返回 IPublishedContent 列表?我正在做类似的事情:-

    mainBlogPageContent.Children.OrderByDescending(x => x.CreateDate).Take(numberOfArticles).ToList()
    

    然后将其传递到我的部分视图中:-

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<IList<IPublishedContent>>
    

    我忘记添加“ToList” - 添加这个解决了我的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-25
      • 1970-01-01
      • 1970-01-01
      • 2015-11-14
      • 2020-11-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多