javascript - 'IJsonHelper' 不包含 'Encode' 的定义

标签 javascript c# json asp.net-core

我想在我看来将字符串列表转换为 javascript 数组,并且我在互联网上的几个地方找到了以下建议:

@model IEnumerable<DSSTools.Models.Box.BoxWhiteListUser>

@{
    var boxManager = new DSSTools.Managers.BoxManager();
    var allusers = boxManager.GetAllUsersAsync().Result.Entries.ToList().Select(u => u.Login);
}

@section Scripts {
    <script>
        var sourcearray = null;

        $(document).ready(function () {
            var arr = JSON.parse('@Html.Raw(Json.Encode(@allusers))');
        });
    </script>
}

但是,我只是觉得很烦

'IJsonHelper' does not contain a definition for 'Encode' and no extension method 'Encode' accepting a first argument of type 'IJsonHelper' could be found (are you missing a using directive or an assembly reference?)

这怎么可能?

我也试过直接打电话

var arr = JSON.parse('@Html.Raw(allusers)');

但是我在控制台中得到了这个错误

VM189:1 Uncaught SyntaxError: Unexpected token S in JSON at position 0
    at JSON.parse (<anonymous>)
    at HTMLDocument.<anonymous> (whitelist:107)
    at fire (jquery.js:3182)
    at Object.fireWith [as resolveWith] (jquery.js:3312)
    at Function.ready (jquery.js:3531)
    at HTMLDocument.completed (jquery.js:3547)

最佳答案

我设法用

做了我需要做的事
sourcearray = JSON.parse('@Html.Raw(Json.Serialize(allusers))');

关于javascript - 'IJsonHelper' 不包含 'Encode' 的定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50056518/

相关文章:

javascript - 保存选定的文本范围以备后用 不起作用

c# - 参数组合的 ArgumentException

javascript - 按一次迭代后停止的日期值对 JSON 数组进行排序

json - 使用继承解析复杂的 JSON 对象

javascript - 根据 url 添加多个值到数据层变量

javascript - jQuery Fullpage - 在菜单上用水平线显示当前部分位置

c# - 具有大数据集的不完整 HttpWebResponse

c# - Rijndael 填充错误

c# - 无法将 Newtonsoft.Json.Linq.JProperty 添加到 Newtonsoft.Json.Linq.JArray

javascript - 列出 Ionic 3 页面上嵌入的 Instagram 帖子