2017-09-11 59 views
0

傳遞我有我自己的手風琴唯一代碼的組件DotVVM - 代碼只組件DataSource屬性爲空時收集

這是我的觀點,我有它加載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(); 
} 

的DTO - 我刪除性能的休息,以明確

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>(); 
} 

回答

1

的問題是,Repeater可能使用客戶端渲染模式(這是默認值) 。當它呈現HTML,它呈現這樣的事情:

<div data-bind="foreach: something"> 
    <!-- template --> 
</div> 

當模板被渲染,其DataContextnull(becasue模板不得包含從項目中的數據 - 這是一個模板)。

所以,你這裏有兩種選擇:通過增加RenderSettings.Mode="Server"Repeater

  1. 打開服務器渲染。
  2. 更新控件,以便在DataContext爲空時不會調用DataBind。
相關問題