ASP.NET MVC 3 Treeview

妖精的绣舞 提交于 2019-11-28 04:47:09

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.

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

    $(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); } }

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!