微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Asp网络核心 – 从模型到javascript

我正在编写一个asp net core应用程序.
我想要实现的是使用 Javascript读取视图中的模型.
我找到了这段代码,但是当我运行它时,我收到此错误

‘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?)

我该怎么办呢?

调节器

public async Task<IActionResult> Index()
{
    return View(await _context.Bolla.ToListAsync());
}

视图

@model IEnumerable<ps0001.Models.Bolla>

<script>
    var bolla = @Html.Raw(Json.Encode(Model));
</script>

解决方法

请在您的视图中尝试使用此功能

@model IEnumerable<ps0001.Models.Bolla>

<script>
    var bolla = '@Html.Raw(Json.Serialize(Model))';
</script>

编辑:

要查看内容,请使用以下内容解析提取的模型:

var parseModel = JSON.parse(bolla);

然后,您将能够使用该对象及其包含的任何属性.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐