我有自己的Accordion仅代码组件
以下是我的视图,其中包含加载list of article sections
的转发器。每篇文章部分都有list of articles
。因此,我希望能够确保每个文章部分都有自己的手风琴,其中包含articles
。这就是我在repeater
<div class="box box-primary">
<dot:Repeater DataSource="{{value: AccordionList}}">
<ItemTemplate>
<coc:Accordion DataSource="{{value: Articles}}"></coc:Accordion>
</ItemTemplate>
</dot:Repeater>
</div>
仅限手风琴代码的组件。即使我清楚地看到DataSource
包含永远为空的AccordionList
List of Articles
,我的DataSource
也始终为空,但永远不会传递到我的AccordionList
。当我将ArticleListDTO
的类型更改为Accordion
并将其直接传递到我的public class Accordion : HtmlGenericControl
{
public Accordion() : base("div")
{
}
public static readonly DotvvmProperty DataSourceProperty;
static Accordion()
{
DataSourceProperty = DotvvmProperty.Register<List<ArticleListDTO>, Accordion>(c=>c.DataSource);
}
//DataSource is always null
public List<ArticleListDTO> DataSource
{
get => (List<ArticleListDTO>)GetValue(DataSourceProperty);
set => SetValue(DataSourceProperty, value);
}
protected override void AddAttributesToRender(IHtmlWriter writer, IDotvvmRequestContext context)
{
Attributes.Add("class", "accordion");
base.AddAttributesToRender(writer, context);
}
public void DataBind(IDotvvmRequestContext context)
{
Children.Clear();
foreach (var item in DataSource)
{
DataBindItem(this, item, context);
}
}....etc
组件时,它运行良好,但这不是我想要的。
public List<ArticleSectionListDTO> AccordionList { get; set; } = new List<ArticleSectionListDTO>();
public List<ArticleSectionListDTO> AccordionListUnsorted { get; set; } = new List<ArticleSectionListDTO>();
protected override void OnItemLoading()
{
AccordionListUnsorted = Task.Run(() => articleSectionFacade.GetAllNotModifiedArticleSections()).Result;
AccordionList = Task.Run(() => articleSectionFacade.CreateTree(AccordionListUnsorted, null)).Result.ToList();
}
视图模型
public class ArticleListDTO
{
public string Name { get; set; }
public int? ParentArticleId { get; set; }
public bool HasCategories => AssignedToArticle?.Count > 0;
public List<ArticleListDTO> AssignedToArticle { get; set; }
//Can contain sub articles
public List<ArticleListDTO> Articles { get; set; } = new List<ArticleListDTO>();
}
public class ArticleSectionListDTO : ListDTO
{
public string Name { get; set; }
public int? ParentArticleSectionId { get; set; }
public bool HasCategories => AssignedToMenuItem?.Count > 0;
public List<ArticleSectionListDTO> AssignedToMenuItem { get; set; }
public List<ArticleListDTO> Articles { get; set; } = new List<ArticleListDTO>();
}
DTO - 我删除了其他属性以使其清晰
vars.getIteration()
答案 0 :(得分:1)
问题是Repeater
可能使用客户端呈现模式(它是默认设置)。当它呈现HTML时,它呈现如下:
<div data-bind="foreach: something">
<!-- template -->
</div>
呈现模板时,其DataContext
为null
(因为模板不得包含项目中的数据 - 它是模板)。
所以你有两个选择:
RenderSettings.Mode="Server"
添加到Repeater
。DataContext
为空时不会调用DataBind。