使用自定义属性过滤 - Umbraco

时间:2016-04-14 17:35:34

标签: c# asp.net-mvc umbraco7

我有一个问题。我需要列出孩子的" Active == true"来自自定义控制器和.cshtml

的属性

控制器

public class PollsController : SurfaceController
{
    private List<Theme> ThemeList()
    {
        var themePageList = Umbraco.Content(CurrentPage.Id).Children.Where("¿¿¿???");
        var themeList = new List<Theme>();

        foreach (var currentTheme in themePageList)
        {
            themeList.Add(
                new Theme
                {
                    ID = currentTheme.Id,
                    Title = currentTheme.Name,
                }
                );
        }

        return themeList;
    }
}

如果不在控制器上过滤,就像我过滤视图一样?

CSHTML

@model Encuestas.ViewModels.PollViewModel

@foreach (var currentTheme in Model.Themes.Where("¿¿¿???")
{
    <div>
        <p>
            <h1 style="text-align: center">@currentTheme.Title</h1>
        </p>
    </div>
}

Umbraco内容

Polls
    Theme 1
        Question 1
            Answer 1
            Answer 2
            Answer 3
            Answer 4
        Question 2
            Answer 1
            Answer 2
            Answer 3
            Answer 4
        Question 3
            Answer 1
            Answer 2
            Answer 3
            Answer 4
    Theme 2 ==> Active == false
        Question 1
            Answer 1
            Answer 2
            Answer 3
            Answer 4
        Question 2
            Answer 1
            Answer 2
            Answer 3
            Answer 4
        Question 3
            Answer 1
            Answer 2
            Answer 3
            Answer 4

THX !!!

修改

public ActionResult RenderPoll()
{

    var viewModel = new PollViewModel
    {
        Themes = ThemeList()
    };

    return PartialView("~/Views/Partials/_Polls.cshtml", viewModel);

}

2 个答案:

答案 0 :(得分:1)

更改此行:

var themePageList = Umbraco.Content(CurrentPage.Id).Children.Where("¿¿¿???");

var themePageList = Umbraco.TypedContent(CurrentPage.Id).Children.Where(x => x.GetPropertyValue<bool>("active"));

Umbraco.Content()是动态的,而Umbraco.TypedContent()是IPublishedContent类型。

您不需要在剃刀文件中过滤。

答案 1 :(得分:0)

@model Encuestas.ViewModels.PollViewModel

@foreach (var currentTheme in Model.Themes(p => p.Active==true))
{
    <div>
        <p>
            <h1 style="text-align: center">@currentTheme.Title</h1>
        </p>
    </div>
}