加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

Asp网络核心 – 从模型到javascript

发布时间:2020-12-16 03:38:38 所属栏目:asp.Net 来源:网络整理
导读:我正在编写一个asp net core应用程序. 我想要实现的是使用 Javascript读取视图中的模型. 我找到了这段代码,但是当我运行它时,我收到此错误: ‘IJsonHelper’ does not contain a definition for ‘Encode’ and no extension method ‘Encode’ accepting a
我正在编写一个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);

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读