Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have done my project with asp.net core 3.1 and I want to show the roles of a group with Jstree  the help

What I want is for each time the tree show shows the roles of that group from the database and the check marks show this, but this does not happen and only shows the tree show.


<pre lang="C#"><pre>
        private void FillTreeView()
        {
            List<TreeViewModel> node = new List<TreeViewModel>();

            node.Add(new TreeViewModel
            {
                id = "1",
                text = "مدیریت دسترسی گروه",
                parent = "#"
            });


            foreach (UserKey keys in _context.UserKeys.Where(kd => kd.ParentID.ToString() != "0"))
            {
                node.Add(new TreeViewModel
                {
                    id = keys.KeyId.ToString(),
                    parent = keys.ParentID.ToString(),
                    text = keys.KeyDesc.ToString()
                });
            }
            ViewBag.SystemPart = JsonConvert.SerializeObject(node);

        }


        [HttpGet]
        public IActionResult AddKeyToGroup(int GroupId, string GroupDesc)
        {
            FillTreeView();
            ViewBag.GroupId = GroupId;
            ViewBag.GroupDesc = GroupDesc;
            ViewBag.groupRole = GetKeyId(GroupId);

            return View();
        }


<pre lang="C#"><pre>        public string GetKeyId(int GroupId)
        {
            var getKey = _context.UserGroupKeys.Where(gk => gk.GroupId == GroupId).ToList();
            string getGroupIdString = "";
            for (int i = 0; i < getKey.Count; i++)
            {
                getGroupIdString += getKey[i].KeyId.ToString() + getGroupIdString;
            }

            return getGroupIdString;
        }


What I have tried:

HTML
<pre>@{
    ViewData["Title"] = " انتساب دسترسی به گروه ";
}

<div class="content">

    <div class="panel panel-heading" style="box-shadow:3px 1px 0 gray; border-radius:2px; background-color:#fff;">
        <span style="font-weight:bold;">
            
            انتساب دسترسی به گروه -  <span style="color:darkgoldenrod;">@ViewBag.GroupDesc</span>
        </span>
    </div>

    <div id="divmsg" style="text-align:center; font-weight:bold" class=""></div>

    <div class="alert alert-info" style="border: 1px solid gray;
        margin-top: 15px;
        text-align: center;
        font-size: 15px;
        background-image: -webkit-gradient(linear, 50% 0%, 50% 100%, color-stop(0%, #1861ac), color-stop(100%, #4a5564));
            color: #fff">
        برای ثبت دسترسی، یک یا چند دسترسی را تیک بزنید
    </div>

    <div class="panel panel-body container-fluid" style="border-radius:2px; box-shadow:0px 1px 3px 0px rgba(0,0,0,0.12);
                display:grid; padding: 3em; background-color:#fff;">
        <form asp-controller="UserGroupKeys" asp-action="AddKeyToGroup" method="post">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <div id="jstreesyspart">

            </div>
            <div class="col-xs-12 form-group" style="margin-top:15px;">
                <input type="submit" style="border:1px solid gray; border-radius:3px;" class="btn pull-right" value="ثبت دسترسی" />
            </div>
        </form>
    </div>

    @if (ViewBag.groupRole != null)
    {
        <input id="rolelist" type="hidden" value="@ViewBag.groupRole" />
    }

    <input name="SelectedItems" id="SelectedItems" type="hidden" />
    <input name="GroupId" id="GroupId" type="hidden" value="@ViewBag.GroupId" />

    <div class="col-xs-12 form-group" style="margin-top:15px;">
        @*<button id="btnsubmit" style="border:1px solid gray; border-radius:3px;"
                    class="btn pull-right">
                ثبت دسترسی
            </button>*@
    </div>

</div>

@section Scripts {
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
    <script>
        $(function () {
            $("#jstreesyspart").on("changed.jstree", function (e, data) {

                var i, j;
                var postedItems = [];

                j = data.selected.length;
                for (var i = 0; i < j; i++) {
                    postedItems.push({
                        text: data.instance.get_node(data.selected[i]).text,
                        id: data.selected[i],
                        parent: data.node.parents[0]
                    });
                    $("#SelectedItems").val(JSON.stringify(postedItems));
                }

            }).jstree({
                "core": {
                    "themes": {
                        "variant":"large"
                    },
                    "data":  @Html.Raw(ViewBag.SystemPart) ,
                },
                "checkbox": {
                    "visable": true,
                    "Keep_selected_style":false
                },
                "plugins": ["checkbox"]
                //checkbox: { three_state: false, cascade:"" }

            }).on("loaded.jstree", function () {
                $.jstree.reference('#jstreesyspart').open_all();

                var urole = $("#rolelist").val().split(",");
                $("#jstreesyspart").jstree(true).select_node(urole);

            });
        });
    </script>
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900