mvc4 razor 递归树

@using Toink.Web.Models;
@using Toink.Web.Common;
@model List<Toink.Web.Models.Category>


@{
    
    ViewBag.Title = "Index";
}


<h2>Index</h2>
@{
    
    var htmlString = string.Empty;


    Html.Render(Model, (render, categories) =>
    {
        if (categories.Count > 0)
        {
            htmlString += "<ul>";


            foreach (var cat in categories)
            {
                htmlString += "<li>";


                htmlString += "<p>" + Html.Encode(cat.Name) + "</p>";
                render(render, cat.Children);


                htmlString += "</li>";
            }


            htmlString += "</ul>";
        }
    });


}


@*展示菜单*@

@Html.Raw(htmlString)


controller端实现逻辑


 //
        // GET: /User/
        public ActionResult Index()
        {
            var model = new List<Category>
            {
                    new Category
                    {
                        Name = "Category 1",
                        Children = new List<Category>
                        {
                            new Category
                            {
                                Name = "Category 1 - 1",
                                Children = new List<Category>()
                            },
                            new Category
                            {
                                Name = "Category 1 - 2",
                                Children = new List<Category>()
                            },
                        }
                    },
                    new Category
                    {
                        Name = "Category 2",
                        Children = new List<Category>
                        {
                            new Category
                            {
                                Name = "Category 2 - 1",
                                Children = new List<Category>()
                            },
                            new Category
                            {
                                Name = "Category 2 - 2",
                                Children = new List<Category>()
                            },
                        }
                    },
                };


            return View(model);
        }


测试实体类


 public class Category
    {
        public string Name { get; set; }


        public List<Category> Children { get; set; }
    }



html扩展方法


//自定义委托

 public delegate void SelfApplicable<T>(SelfApplicable<T> self, T arg);


    public static class HtmlExtensions
    {
        public static void Render<T>(this HtmlHelper helper, T model, SelfApplicable<T> f)
        {
            f(f, model);
        }


        private static Action<T> Fix<T>(Func<Action<T>, Action<T>> f)
        {
            return x => f(Fix(f))(x);
        }


        public static void Render<T>(this HtmlHelper helper, T model, Func<Action<T>, Action<T>> f)
        {
            Fix(f)(model);
        }
    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值