Cloud + IoT + Devices + Mixed Reality

Rendering a tree view using ASP.NET MVC 3 + Razor

I modified the Html Helper created by Mike Hadlow based on the answered provided by marcind over on Stack Overflow and it works pretty beautifully.

I also made a couple “improvements”:

Updated to use the HtmlTextWriter provided by the HtmlHelper so that it is compatible with MVC 3.

Added a children expression so you don’t need the IComposite<T> interface

Added a URL expression so you can specify a URL for the content of the <li> element

I wanted to use generated classes as the source so I didn’t feel like implementing the IComposite<T> interface and breaking the code generation (or investing the time to alter the code generation just to implement this custom interface). Therefore, I added the ability to specify an expression that returns a generic list List<T>.

Can you please provide me the table that you used. Basically What I need to understand is how the model for the razor is generated, i.e. how is the parent child relationship getting established. That is why if you could provide the source code of the project, it would be great.

I remember using his post and the result was html markup TEXT rendered in browser, not some elements. As far as I can tell, this code still returns simple string from a helper, not an HtmlMvcString. believe me I can post code without looking at it too, it’s just I see no point in doing so

I encountered the same issue, which as you can see from my code I return string.Empty. rather than what Milke returned which was writer.InnerWriter.ToString(). Check out his Render() method and compare it to mine.

Thank you for the learning experience. After noodling through your code and having Stack Overflow issues I gave up and relooked at Matt Hidinger’s NuGet install-package MvcTreeView. Along with jQuery’s TreeView it’s a sweet solution that was easy to implement. I build my tree structure using SQL Server HierarchyID and use the string version for my primary key.

Mark, thank you for post! I have next error, when i use RenderTree function in my view: “Argument-types for a method “LastTry.HtmlHelpers.TreeViewHelper.RenderTree (System.Web.Mvc.HtmlHelper, System.Collections.Generic.List , System.Func , System.Func <T, System.Collections.Generic.List >, System.Func ) “should not be determined for use. Try to clearly define the argument types.”. Do you khow what it means?