4

I am creating an application in that I need to create Treeview for that I take reference of this site

Actually this project is created in MVC Framework and currently, I am using ASP.NET CORE(v3.0) and when I try to send JSON using JavaScriptSerializer than it shows me an error that's why I am using Newtownsoft Json for convert List class to JSON.

Create.CSHTML

<div class="row">

    <div class="col-md-4">
        <form asp-action="Create">
            <div id="jstree">
            </div>
            <input type="hidden" name="selectedItems" id="selectedItems" />
            <input type="submit" value="Submit" />
        </form>
    </div>
</div>

<div>
    <a asp-action="Index">Back to List</a>
</div>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.2.1/themes/default/style.min.css" />
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.1/jquery.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.2.1/jstree.min.js"></script>
<script type="text/javascript">
    $(function () {
        $('#jstree').on('changed.jstree', function (e, data) {
            var i, j;
            var selectedItems = [];
            for (i = 0, j = data.selected.length; i < j; i++) {

                //Fetch the Id.
                var id = data.selected[i];

                //Remove the ParentId.
                if (id.indexOf('-') != -1) {
                    id = id.split("-")[1];
                }

                //Add the Node to the JSON Array.
                selectedItems.push({
                    text: data.instance.get_node(data.selected[i]).text,
                    id: id,
                    parent: data.node.parents[0]
                });
            }

            //Serialize the JSON Array and save in HiddenField.
            $('#selectedItems').val(JSON.stringify(selectedItems));
        }).jstree({
            "core": {
                "themes": {
                    "variant": "large"
                },
                "data": @ViewBag.Data
            },
            "checkbox": {
                "keep_selected_style": false
            },
            "plugins": ["wholerow", "checkbox"],
        });
    });
</script>

MyContoller.cs

public ActionResult Create()
{
    //Loop and add the Parent Nodes.
    List<Menu> ParentMenus = _context.Menu.Where(t => t.ParentID == null).ToList();
    foreach (Menu type in ParentMenus)
        MenuBinding.Add(new TreeViewNode { id = type.MenuID.ToString(), parent = "#", text = type.Title });
    List<Menu> ChildMenus = _context.Menu.Where(t => t.ParentID != null).ToList();
    //Loop and add the Child Nodes.
    foreach (Menu subType in ChildMenus)
    {
        MenuBinding.Add(new TreeViewNode { id = subType.MenuID.ToString(), parent = subType.ParentID.ToString(), text = subType.Title });
    }

    //Serialize to JSON string.
    //ViewBag.Json = (new JavaScriptSerializer()).Serialize(nodes);
    string JsonData = JsonConvert.SerializeObject(MenuBinding);
    ViewBag.Data = JsonData;
    return View();
}

In js side when I get data it is in an escape sequence when I try to unescape and than try to send that data then also this gives me an error.

Actual JSON:

[
    {
        "id":"2",
        "parent":"#",
        "text":"A"
    },
    {
        "id":"5",
        "parent":"#",
        "text":"B"
    },
    {
        "id":"3",
        "parent":"2",
        "text":"C"
    },
    {
        "id":"4",
        "parent":"2",
        "text":"D"
    }
]

Get JSON in javascript:

[
    {
        &quot;id&quot;:&quot;2&quot;,
        &quot;parent&quot;:&quot;#&quot;,
        &quot;text&quot;:&quot;A&quot;
    },
    {
        &quot;id&quot;:&quot;5&quot;,
        &quot;parent&quot;:&quot;#&quot;,
        &quot;text&quot;:&quot;B&quot;
    },
    {
        &quot;id&quot;:&quot;3&quot;,
        &quot;parent&quot;:&quot;2&quot;,
        &quot;text&quot;:&quot;C&quot;
    },
    {
        &quot;id&quot;:&quot;4&quot;,
        &quot;parent&quot;:&quot;2&quot;,
        &quot;text&quot;:&quot;D&quot;
    }
]

Can anyone please look into this and suggest me what should I have to change in my code?

Deep Soni
  • 431
  • 4
  • 24
  • You haven’t shown any code or errors or anything so we can’t suggest anything related to your code. Please include at least the related parts, or an [mcve] if possible. – Sami Kuhmonen Oct 25 '19 at 07:48
  • @SamiKuhmonen THi, Thanks for the reply. Actually I have same code as I mention in the link and data is get encrypted on the javascript side and that's my issue. – Deep Soni Oct 25 '19 at 07:50
  • So what do you get that’s “encrypted”, what’s wrong with it, how should it be instead etc? If there’s working code on some site that just doesn’t work for you but you don’t give specifics on how it doesn’t work there still isn’t much we can say. – Sami Kuhmonen Oct 25 '19 at 07:54
  • Wait I add actual result and result in JSON in my question. – Deep Soni Oct 25 '19 at 07:57
  • Where is the code?? – Moo-Juice Oct 25 '19 at 08:02
  • I added JSON for better understanding Please review the link there is no change except I am using .NET Core. – Deep Soni Oct 25 '19 at 08:05
  • @DeepSoni This is not a site for us to guess what you were doing with the code. – Moo-Juice Oct 25 '19 at 08:07
  • @Moo-Juice Wait I just add code in question. – Deep Soni Oct 25 '19 at 08:09
  • why on earth would you want to do that? –  Oct 25 '19 at 08:47

1 Answers1

2

There is little mistake in script.

<script type="text/javascript">
$(function () {
    $('#jstree').on('changed.jstree', function (e, data) {
        var i, j;
        var selectedItems = [];
        for (i = 0, j = data.selected.length; i < j; i++) {

            //Fetch the Id.
            var id = data.selected[i];

            //Remove the ParentId.
            if (id.indexOf('-') != -1) {
                id = id.split("-")[1];
            }

            //Add the Node to the JSON Array.
            selectedItems.push({
                text: data.instance.get_node(data.selected[i]).text,
                id: id,
                parent: data.node.parents[0]
            });
        }

        //Serialize the JSON Array and save in HiddenField.
        $('#selectedItems').val(JSON.stringify(selectedItems));
    }).jstree({
        "core": {
            "themes": {
                "variant": "large"
            },
            "data": @Html.Raw(ViewBag.Data)//Here need to add Html.Raw instead of ViewBag.Data
        },
        "checkbox": {
            "keep_selected_style": false
        },
        "plugins": ["wholerow", "checkbox"],
    });
});
</script>
Deep Soni
  • 431
  • 4
  • 24