mvc view - 无法访问嵌套列表

时间:2015-07-15 06:46:48

标签: c# asp.net-mvc razor

我有一个类别列表,每个类别可能有子类别。我在控制器中获得了具有正确记录的列表,但如果我尝试在我的视图中使用子类别列表,则它什么也没有显示。

类:

 public class DashboardTile
{
    public int ID { get; set; }
    public int? CategoryID { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
}
public class DashboardTileBO : DashboardTile
{
    public bool IsChecked { get; set; }
    public List<DashboardTileBO> DashboardTiles { get; set; }
}

public class AddTileVM
{
    public List<DashboardTileBO> DashboardTiles { get; set; }
}

控制器:

 public ActionResult AddTiles()
    {
        var allDashBoardTiles = BLL.PublicLayer.GetAllDashBoardTiles(SessionItems.UserId);
        var addTileVm = new AddTileVM()
        {
            DashboardTiles = allDashBoardTiles,
        };
        return PartialView(addTileVm );
    }

查看:

 @model Hugo.BusinessObjects.AddTileVM
 <link href="~/Content/js/nestable/nestable.css" rel="stylesheet"/>
 <script src="~/Content/js/nestable/jquery.nestable.js"></script>
 <div class="modal-dialog">
<div class="modal-content">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title">Add/Remove Tiles to desktop</h4>
    </div>
     @using (Ajax.BeginForm("AddTiles", "Default",new AjaxOptions {OnSuccess = "OnSuccess" }))
    {
        @Html.AntiForgeryToken();
        <div class="modal-body">
            <div class="row">
                <div class="col-lg-12">
                    @for (var i = 0; i < Model.DashboardTiles.Count; i++)
                    {
                        <div class="col-lg-5">
                            <label class="">
                                @Html.CheckBoxFor(a=> Model.DashboardTiles[i].IsChecked)
                                @Model.DashboardTiles[i].Description
                            </label>
                        </div>
                        @*if (@Model.DashboardTiles[i].DashboarTiles.Count > 0)
                        {
                            for (var j = 0; j < @Model.DashboardTiles[i].DashboarTiles.Count; j++)
                             {
                                <div class="col-lg-5">
                                    <label class="">
                                        @Html.CheckBoxFor(a => @Model.DashboardTiles[i].DashboarTiles[j].IsChecked)
                                        @Model.DashboardTiles[i].DashboarTiles[j].Description
                                    </label>
                                </div>
                            }
                        }*@
                        @Html.HiddenFor(a=> Model.DashboardTiles[i].ID)
                       }
                </div>
            </div>

        </div>

        <div class="modal-footer">
            <button type="button" class="btn btn-rounded btn-sm btn-tiles" data-dismiss="modal">Cancel</button>
            <button type="submit" class="btn btn-rounded btn-sm btn-tiles" >Update</button>
        </div>
    }
</div><!-- /.modal-content -->
}

获取列表的方法

   public static List<BusinessObjects.DashboardTileBO> GetAllDashBoardTiles(long userId)
    {
        using (var context = new HugoInternalEntities())
        {
var distincCategoryIds = context.DashboardTiles.Select(a => a.CategoryID).Distinct().ToList();
            var allTiles = (context.DashboardTiles.Where(a => distincCategoryIds.Contains(a.ID)).Select(x=>
                            new BusinessObjects.DashboardTileBO
                            {
                                ID = x.ID,
                                Name = x.Name,
                                Description = x.Description,
                            })).ToList();
            var list = context.DashboardUserTiles.Where(a => a.UserID == userId).Select(a => a.DashboardTileID).ToList();

            allTiles.ForEach(a => a.IsChecked = list.Contains(a.ID));
            allTiles.ForEach(a=>a.DashboardTiles=context.DashboardTiles.Where(b=>a.ID==b.CategoryID && b.ID != a.ID).Select(x=>
            new BusinessObjects.DashboardTileBO
            {
                ID = x.ID,
                Name = x.Name,
                Description = x.Description,
            }).ToList());
            allTiles.ForEach(a => a.DashboardTiles.ForEach(b => b.IsChecked = list.Contains(b.ID)));
            return allTiles;
        }
    }

在上面的视图中显示了我的主要类别列表,但如果我取消注释子类别的代码,则不会显示任何内容。请参阅我在addTileVm

中获得的列表的屏幕截图

瓷砖列表:

enter image description here

0 个答案:

没有答案