如何使用 Razor 将未编码的 Json 写入我的视图? [英] How do I write unencoded Json to my View using Razor?

查看:29
本文介绍了如何使用 Razor 将未编码的 Json 写入我的视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Razor 将一个对象作为 JSON 写入我的 Asp.Net MVC 视图,如下所示:

I'm trying to write an object as JSON to my Asp.Net MVC View using Razor, like so:

<script type="text/javascript">
  var potentialAttendees = @Json.Encode(Model.PotentialAttendees);
</script>

问题是在输出中对 JSON 进行了编码,而我的浏览器不喜欢它.例如:

The problem is that in the output the JSON is encoded, and my browser doesn't like it. For example:

<script type="text/javascript">
    var potentialAttendees = [{&quot;Name&quot;:&quot;Samuel Jack&quot;},];
</script>

如何让 Razor 发出未编码的 JSON?

How do I get Razor to emit unencoded JSON?

推荐答案

你要做:

@Html.Raw(Json.Encode(Model.PotentialAttendees))

在 Beta 2 之前的版本中,您是这样做的:

In releases earlier than Beta 2 you did it like:

@(new HtmlString(Json.Encode(Model.PotentialAttendees)))

这篇关于如何使用 Razor 将未编码的 Json 写入我的视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆