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

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

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....