【问题标题】:Display Nested Data (hierarchy), MVC3显示嵌套数据(层次结构),MVC3
【发布时间】:2014-03-24 06:25:38
【问题描述】:

我的模型如下:

public class Category
{
    [Key]
    public int Id { get; set; }

    [StringLength(200), Required, DataType(DataType.Text)]
    public string Title { get; set; }

    [Display(Name = "Parent Category")]
    public int? ParentId { get; set; }

    [DisplayFormat(NullDisplayText = "Root")]
    public virtual Category ParentCategory { get; set; }

    public virtual ICollection<Category> ChildCategories { get; set; }
}

本质上是分层的,“ParentId”引用“Id”作为自引用。有了这个,我试图按如下方式显示数据:

+--------+-----------------+
| S. No  |        Name     |
+--------+-----------------+
|   1    |  Parent One     |
+--------+-----------------+
|   1.1  |  Child-1 of One |
+--------+-----------------+
|   1.2  |  Child-2 of One |
+--------+-----------------+
|   2    |  Parent Two     |
+--------+-----------------+
|   2.1  |  Child-1 of Two |
+--------+-----------------+
|   3    |  Parent Three   |
+--------+-----------------+

请帮帮我。

【问题讨论】:

    标签: asp.net-mvc-3 model views hierarchy


    【解决方案1】:

    您可以通过 parentId 使用组来计算总子数,然后计算

    这是我的解决方案

    定义一个视图模型

    public class CategoryViewMoel
    {
        public IEnumerable<ParentCategoryInfo> parentCategoryInfo { get; set; }
        public int SN { get; set; }
        public int ChildSN { get; set; }
    }
    
    public class ParentCategoryInfo
    {
        public int? ParentId { get; set; }
        public int TotalChild { get; set; }
    }
    

    在控制器中

    var model = new CategoryViewMoel();
    
    var parentCategory =  from r in db.Categories
                          where r.ParentCategory != null
                          group r by r.ParentId into newGroup
                          select new ParentCategoryInfo 
                          {
                              TotalChild = newGroup.Count(),
                              ParentId = newGroup.Key,
                          };
    model.SN = 1;
    model.ChildSN = 1;
    model.parentCategoryInfo = parentCategory;
    

    最后在你的视野中

    <table>
    <thead>
        <tr>
            <th>S. No</th>
            <th>Name</th>
        </tr>
    </thead>
    <tbody>
            @foreach (var parent in Model.parentCategoryInfo)
            {
                <tr>
                    <td>@Model.SN </td>
                    <td>Parent @Model.SN</td>
                </tr>
    
                for (var i = Model.ChildSN;  i <= parent.TotalChild; i++)
                {
                    <tr>
                        <td>@Model.SN.@i</td>
                        <td>Child - @i of @Model.SN</td>
                    </tr>
    
                Model.ChildSN = 1;
                Model.SN++;
            }
    </tbody>
    

    数字到单词的映射可以通过构建一个自定义的辅助函数来实现,SO中有很多例子

    converting numbers in to words C#

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-12-20
      • 2015-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-01
      相关资源
      最近更新 更多