【问题标题】:How to send Json Data from controller to javascript without in ASP.NET Core?如何在没有 ASP.NET Core 的情况下将 Json 数据从控制器发送到 javascript?
【发布时间】:2020-02-21 13:20:56
【问题描述】:

我正在创建一个应用程序,我需要为此创建 Treeview,我参考了 this site

实际上,这个项目是在 MVC 框架中创建的,目前,我使用的是 ASP.NET CORE(v3.0),当我尝试使用 JavaScriptSerializer 发送 JSON 时,它向我显示了一个错误,这就是我使用 Newtownsoft Json 进行转换的原因将类列出为 JSON。

创建.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();
}

在 js 端,当我获取数据时,它处于转义序列中,当我尝试取消转义而不是尝试发送该数据时,这也会给我一个错误。

实际 JSON:

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

在 javascript 中获取 JSON:

[
    {
        &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;
    }
]

任何人都可以看看这个并建议我应该在我的代码中进行哪些更改?

【问题讨论】:

  • 您没有显示任何代码或错误或任何内容,因此我们无法建议与您的代码相关的任何内容。请至少包含相关部分,如果可能,请提供minimal reproducible example
  • @SamiKuhmonen THi,感谢您的回复。实际上,我有与链接中提到的相同的代码,并且数据在 javascript 端被加密,这是我的问题。
  • 那么你得到了什么是“加密的”,它有什么问题,它应该如何替代等等?如果某个网站上的工作代码对您不起作用,但您没有具体说明它是如何不起作用的,那么我们也无能为力。
  • 等待我添加实际结果并在我的问题中生成 JSON。
  • 代码在哪里??

标签: c# asp.net asp.net-mvc asp.net-core jsonserializer


【解决方案1】:

脚本有小错误。

<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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-03
    • 2020-10-08
    • 2014-08-16
    • 2015-12-17
    • 2020-04-09
    • 2020-10-21
    相关资源
    最近更新 更多