开发者

Recursive functions in MVC View aspx page

I have a recursive data structure that I want ASP.NET to walk in the View part of an MVC prog开发者_StackOverflowram. It's not clear to me whether this is even possible. Here's the definition of the model class in C#:

Public class recursive_data_structure
{
   public List<recursive_data_structure> Children;
   //some_class is defined elsewhere, it is irrelevant
   public some_class Me;
}

The ViewModel includes an instance of this class. Is there a way in ASP.NET (using MVC) to walk the model so that I can have some sort of recursive HTML generation?

Apologies if this question is poorly phrased or obvious. I am very new to MVC.


You try to render hierarchical tree, there are plenty ways to do this in ASP.NET MVC. You can use partial view for this as follows.

Here is your simple hierarchical model

public class HierarchicalModel
{
    public HierarchicalModel()
    {
        this.Children = new List<HierarchicalModel>();
    }

    public String Name { get; set; }
    public List<HierarchicalModel> Children { get; set; }
}

then in one of actions you create/fetch your model and put it in a viewbag.

public ActionResult Index()
{
    ViewBag.Message = "Welcome to ASP.NET MVC!";

    var level0 = new HierarchicalModel() { Name = "item 0, level 0" };
    level0.Children.Add(new HierarchicalModel() { Name = "item 0, level 1" });
    level0.Children.Add(new HierarchicalModel() { Name = "item 1, level 1" });
    level0.Children.Add(new HierarchicalModel() { Name = "item 2, level 1" });
    var level1 = new HierarchicalModel() { Name = "item 3, level 1" };
    level1.Children.Add(new HierarchicalModel() { Name = "item 0, level 2" });
    level1.Children.Add(new HierarchicalModel() { Name = "item 1, level 2" });
    level0.Children.Add(level1);

    ViewBag.HierarchicalModel = level0; 

    return View();
}

Now you need to create a partial view named e.g. _HierarchicalModel.cshtml in Views/Shared directory. Replace MvcApplication4 by the namespace where your model is located.

@model MvcApplication4.HierarchicalModel
<li>
    @Model.Name
    <ul>
    @foreach (var item in Model.Children)
    {
        @Html.Partial("_HierarchicalModel", item);  
    }    
    </ul>
</li>

and then you render it in your view.

@{
    MvcApplication4.HierarchicalModel model = ViewBag.HierarchicalModel; 
}

<p>
    <ul>@Html.Partial("_HierarchicalModel", model)</ul>
</p>

This is a simplest way to do this. In any case you have to care about cyclic reference and deep hierarchies.


To my limited knowledge, MVC won't walk a recursive structure automatically for you.

What you need to do is to create a partial view for List or IEnumerable and another for recursive_data_structure.

In your partial view for IEnumerable model:

@foreach (recursive_data_structure child in Model)
{ 
    @Html.Partial("recursive_data_structureView", child)  
}

And in your partial view for recursive_data_structure model:

<span>Model</span>

If you want a treeview that binds to recursive data, Telerik has one as part of their free MVC package.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜