Umbraco 中的部分模型

Model to Partial in Umbraco

我的“关于我们”页面上有以下片段(为清楚起见已缩短):

@foreach(var employee in CurrentPage.Employee) {

                  <img class="img-fluid" src="@Umbraco.Media(Convert.ToString(employee.Photo)).umbracoFile" alt="" />


                <h2>@employee.Name</h2>
                <h3>@employee.Title</h3>
                <span>@employee.Bio</span>
        }

foreach 的主体实际上更大,我特意选择在片段中保留图像以显示@Umbraco.Media

的用法

所以我想把它解压成一个Partial。 这是部分代码:

@inherits Umbraco.Web.Mvc.UmbracoTemplatePage<ContentModels.Employee>
@using ContentModels = Umbraco.Web.PublishedContentModels;
@using Umbraco.Web;

<img class="img-fluid" src="@Umbraco.Media(Convert.ToString(Model.Content.Photo)).umbracoFile" alt="" />

    <h2>@Model.Content.Name</h2>
    <h3>@Model.Content.Title</h3>
    @Model.Content.Bio

然后页面变成这样:

@foreach(var employee in CurrentPage.Employee) {

   @Html.Partial("Employee", new global::Umbraco.Web.Models.RenderModel(employee, Model.CurrentCulture))
        }

问题是我得到了

 Cannot bind source content type Umbraco.Web.Models.DynamicPublishedContent to model content type Umbraco.Web.PublishedContentModels.Employee.

我认为我的问题是因为我用 CurrentPage.Employee 遍历了我的员工,而不是像 Model.Children.

这样的东西

无论如何,有没有办法从 DynamicPublishedContent 中获取模型?

我发现了我的问题所在。 实际上,根据经验,您不应该使用 CurrentPage

我将“关于我们”页面中的代码更改为:

 @foreach(var employee in Model.Content.Children("Employee")) {
     @Html.Partial("Employee", employee)
 }

那么我的局部视图需要这样开始:

@inherits Umbraco.Web.Mvc.UmbracoTemplatePage<ContentModels.Employee>
@using ContentModels = Umbraco.Web.PublishedContentModels;

然后我可以在 Employee 上使用 @Model.Name 或任何其他文档类型 属性。

就是这样!