10

I need to display a Treeview in my MVC3 application. There will be a self referencing hierarchical table (Folders) and another table linked to it (Documents.) (So Folders can have N-subFolders and any folder/sub folder can have many documents.)

I have looked into using third party vendors such as Telerik, DJME and MVC Controls Toolkit. While all nice packages, I'm uneasy about the licences, and since i'm new to MVC (and programming in general,) I find their documentation lacking to get the right display working.

I've also looked at the heavily referenced blogs on TreeViews:

TreeViewHelper and the Recursive Partial View

In addition to the other less referenced articles (The top 3 are also very informative):

  1. http://tpeczek.com/2010/01/asynchronous-treeview-in-aspnet-mvc.html
  2. http://mikehadlow.blogspot.com/2008/10/rendering-tree-view-using-mvc-framework.html
  3. http://www.tek-tips.com/viewthread.cfm?qid=1637392&page=4
  4. http://weblogs.asp.net/jigardesai/archive/2008/02/04/display-hierarchical-data-in-asp-net-mvc-framework.aspx
  5. http://www.jigar.net/articles/viewhtmlcontent311.aspx
  6. http://help.syncfusion.com/ug_82/ASP.NETMVCUI_Tools/CreatingATreeViewControl.html

I would like to use either the TreeViewHelper or the Recursive Partial View Method.
However, in the TreeViewHelper, I can't make it pull data from the second table (ie. I can only make it list the Files, but I'm not sure how to have it list the Documents for each File.)
For the Recursive Partial View, I'm still at a loss in how to convert this to MVC3 and also general implementation. I did find a post (forums.asp.net/t/1652809.aspx/1?treeview+with+mvc+3) that gives an explanation of how to convert a bit of it to MVC3, but i'm still unclear of what to do with it. I keep getting the error for the Partial view: Cannot implicitly Convert type 'void' to type 'object'

Like I said before I'm new to MVC3 and would like insight in which method would work best for my scenario and how to implement it.

tpeczek
  • 23,867
  • 3
  • 74
  • 77
James
  • 1,158
  • 4
  • 13
  • 23
  • By using Html.Render and Html.Partial instead of Html.RenderAction and Html.RenderPartial respectively, the "Cannot implicitly Convert type 'void' to type 'object'" error goes away – James Apr 06 '11 at 20:24

3 Answers3

5

In case anyone is wondering, the way I solved this problem was to use a recursive partial view. The problem I has having with it was that I didn't have the self referencing relationship set up in SQL/EF (I just had the ParentID field which wasn't linked to the Primary Key.) I also integrated jsTree as this has a lot of slick functionality such as search.

Like I said in the comment above, @Html.Action and @Html.Partial work instead of @Html.RenderAction and @Html.RenderPartial.

James
  • 1,158
  • 4
  • 13
  • 23
0
    $(document).ready(function () {
        BindChart();
    });
    function BindChart() {
        $("#org").jOrgChart({
            chartElement: '#chart',
            dragAndDrop: true
        });
    }
    $(".cardadd").live("click", function ()
    {
        var data = { id: 0 , ParentId:$(this).parent().data('cardid')};
        OpenForminWindow('divfrmChartMember', 'divChartMember', 'frmChartMember', chart.ChartMember, data, '', 400, 1000);
    });
    $(".cardedit").live("click", function () {
        var data = { id: $(this).parent().data('cardid')};
        OpenForminWindow('divfrmChartMember', 'divChartMember', 'frmChartMember', chart.ChartMember, data, '', 400, 1000);
    });

    $(".cardremove").live("click", function () {

    });
    function OpenForminWindow(popupId, targetDivId, formid, url, data, callbackfunc, heigth, width) {
        $.ajax({
            type: "GET",
            url: url,
            data: data,
            cache: false,
            success: function (data) {
                $('#' + targetDivId).html(data);
                $('#' + formid).removeData('validator');
                $('#' + formid).removeData('unobtrusiveValidation');
                $('#' + formid).each(function () { $.data($(this)[0], 'validator', false); }); //enable to display the error messages
                $.validator.unobtrusive.parse('#' + formid);
                if (callbackfunc)
                    return callbackfunc();
            }
        });

        $("#" + popupId).dialog({
            modal: true,
            height: heigth,
            width: width,
            beforeClose: function (event, ui) {
                if (typeof refresh !== 'undefined' && refresh == true)
                    ReloadCurrentPage();
            }
        });
    }
    $('#frmChartMember').live('submit', function (e) {
        SubmitAjaxForm($(this).attr('id'), chart.AddMember, ReloadChart);
        e.preventDefault();
    });
    function SubmitAjaxForm(formId, url, callBack) {
        $.ajax({
            url: url,
            type: 'post',
            cache: false,
            data: $('#' + formId).serialize(),
            success: function (data) {
                return callBack(data);
            },
        });
    }
    function ReloadChart(result) {
        ClosePopup('divfrmChartMember');
        $.ajax({
            type: 'GET',
            url: chart.ChartList,
            cache: false,
            success: function (result) {
                $("#orgChart").html(result);
                BindChart();

            }
        });
    }
    function ClosePopup(divid) {
        $("#" + divid).dialog("close");

    }

public class ChartController : Controller { // // GET: /Chart/ ChartContext ctx = new ChartContext(); public ActionResult Index() { return View(); } public ActionResult OrgChart() { return PartialView("_OrgChart", ctx.Cards.ToList()); } public ActionResult ChartMember(int id, int? ParentId = null) { Card card = new Card(); if (id > 0) card = ctx.Cards.Find(id); else card.ParentId = ParentId; return PartialView("_ChartMember", card); } public ActionResult SaveMember(Card card) { if (card.id == 0) ctx.Cards.Add(card); else ctx.Entry(card).State = System.Data.EntityState.Modified; ctx.SaveChanges(); return Json(true, JsonRequestBehavior.AllowGet); } }

arjthakur
  • 61
  • 6
0

give a look to the edit/add/delete/node move templated TreeView of my Mvc Controls Toolkit here: http://mvccontrolstoolkit.codeplex.com/wikipage?title=TreeView

Francesco Abbruzzese
  • 4,139
  • 1
  • 17
  • 18
  • I have not yet switched over to the TreeView in the MVC Controls Toolkit. However, I am going to need to incorporate a more sophisticated Tree structure in my application; so this looks to be a great option! While now, the documentation for the Tree View makes sense, when I was starting out, the documentation was overwhelming. I would suggest a quick video or simplified sample to help all the kids starting out – James Mar 09 '12 at 02:52
  • At moment the only sample available is the one contained in the examples that comes together with the binary distribution on Codeplex. We are preparing a demo web site featuring all controls. Each control will have some working examples with the associated source code...in a way similar to jQuery UI samples. Moreover we made agreement with companies for the production of easy to use plugins that that will make the use of controls easier, more specifically they will contain sets of already configured controls put together to accomplish the more common uses with a theme roller to style them. – Francesco Abbruzzese Mar 09 '12 at 10:36
  • I advice you read the section on templates as first step: http://mvccontrolstoolkit.codeplex.com/wikipage?title=Use%20of%20Templates Then read the doc, and play a little with the working sample of the treeview...If you have questions you can post them here: http://mvccontrolstoolkit.codeplex.com/discussions – Francesco Abbruzzese Mar 09 '12 at 10:38
  • One quick suggestion that I came across recently (which help me out a little and help a peer who is just starting out in the MVC world tremendously) was the the screen cast for the MvcContrib Grid http://www.userinexperience.com/?p=283 I've found that beginners jump in leaps and bounds when there is a video around. – James Mar 15 '12 at 00:54
  • We planned also videos. However, the first step is the web site with all demos. Then, we will use the example there to prepare some quick start video....Preparing good quickstart videos is not easy and requires time. A good video can be done just after the selection of "strategic examples"...that we will test on the web site. We need some months to prepare all this. Now we are very busy moving to Mvc 4. We will start working on all these stuffs after the first beta targetted to Mvc 4 will be ready. – Francesco Abbruzzese Mar 15 '12 at 12:50