【问题标题】:Umbraco: Cannot bind source content type ArticleItem to model content type ArticlesMainUmbraco:无法将源内容类型 ArticleItem 绑定到模型内容类型 ArticlesMain
【发布时间】:2018-07-06 10:15:17
【问题描述】:

这是我们第一次尝试 Umbraco,我们正在按照官方教程创建一个北极/新闻功能作为示例。

我们基本上都是从上面开始here

所以,首先我们创建了一个主页和一个母版页,以及介于两者之间的所有内容,直到我们到达this 点。

显示的一切都正常,我们有一个概览,宏/局部视图运行良好。

但是当我们想要导航到实际的 ArticleItem 时,我们遇到了标题中提到的错误。

无法将源内容类型 Umbraco.Web.PublishedContentModels.ArticlesItem 绑定到模型内容类型 Umbraco.Web.PublishedContentModels.ArticlesMain。视图和内容模型都是 PureLive,具有相同的版本。应用程序处于不稳定状态,应重新启动。

现在,我的猜测是 Masterpage 作为 ArticlesMain 模板是一个问题,这会强制使用该模型/将其传递给 ArticlesItem 页面。但是我的 MVC 已经很生锈了,所以我现在似乎无法自己找到解决方案。

使用的代码是从教程中 1:1 复制的,以确保我们没有搞砸任何东西,但到目前为止还没有骰子。我们还在 Umbraco CMS 中做了所有事情,我们的 IDE (VS) 没有被使用。

/Views/ArticlesMain.cshtml

@inherits Umbraco.Web.Mvc.UmbracoTemplatePage
<ContentModels.ArticlesMain>
  @using ContentModels = Umbraco.Web.PublishedContentModels; @{ Layout = "HomePage.cshtml"; }
  <div id="main-container">
    <div id="main" class="wrapper clearfix">
      <section>
        <h2>@Umbraco.Field("articlesTitle")</h2>
        <p>@Umbraco.Field("articlesBodyText")</p>
        <p>@Umbraco.RenderMacro("listArticles")</p>
      </section>
    </div>
    <!-- #main -->
  </div>
  <!-- #main-container -->

/Views/ArticlesItem.cshtml

@inherits Umbraco.Web.Mvc.UmbracoTemplatePage
<ContentModels.ArticlesItem>
  @using ContentModels = Umbraco.Web.PublishedContentModels; @{ Layout = "ArticlesMain.cshtml"; }

  <h1>@Umbraco.Field("articlesTitle")</h1>
  <article>
    <h2>@Umbraco.Field("createDate")</h2>
    <p>@Umbraco.Field("articleContents")</p>
  </article>

/Views/MacroPartials/listArticles.cshtml

@inherits Umbraco.Web.Macros.PartialViewMacroPage
@{ 
    var selection = CurrentPage.Children.Where("Visible").OrderBy("CreateDate desc"); 
    @* OrderBy() takes the property to sort by and optionally order desc/asc *@
}

@foreach (var item in selection)
{
    <div class="article">
        <div class="articletitle"><a href="@item.Url">@item.Name</a></div>
        <div class="articlepreview">@Umbraco.Truncate(@item.ArticleContents,100) <a href="@item.Url">Read More..</a></div>
    </div>
    <hr/>
}

至于文档类型,它们看起来与教程的屏幕截图完全相同,我当然也可以提供这些,包括设置的权限。

【问题讨论】:

  • 您在哪里以及如何使用 ArticlesItem.cshtml?您应该只有 ArticlesMain 模板,在其中您将使用该页面的子级呈现宏。就是这样。

标签: html asp.net-mvc model umbraco


【解决方案1】:

我从 Umbraco 官方论坛获得了一些帮助,结果证明我确实犯了一个简单的错误。

如果有人偶然发现这个问题,我就把它留在这里:

这是因为视图中使用的模型类型不正确。您在 ArticleDetails 视图中使用了 Layout = "ArticleMain.cshtml",这导致 ArticleMain.cshtml 被用作父视图。这个视图需要一个 ArticleMain 类型的模型。切换到“Layout=Master.cshtml”应该可以解决问题。至少这似乎是教程中的主视图。

Umbraco forum comment

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-05
    • 1970-01-01
    • 2017-11-22
    • 1970-01-01
    • 2014-08-25
    相关资源
    最近更新 更多