I have a recursive data structure that I want ASP.NET to walk in the View part of an MVC program. 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
then in one of actions you create/fetch your model and put it in a viewbag.
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.
and then you render it in your view.
This is a simplest way to do this. In any case you have to care about cyclic reference and deep hierarchies.