[英]Invalid property ID error - JSON object from List<> c# ASP.NET
I've been on this for a couple of days now and getting nowhere. 我已经在这上呆了几天了,却一无所获。
I'm trying to serialize a List object to JSON so it can be passed to Google Analytics' e-commerce service, so it needs to get passed into javascript beforehand. 我正在尝试将List对象序列化为JSON,以便可以将其传递给Google Analytics(分析)的电子商务服务,因此需要事先将其传递给javascript。
However, there is an error with the json output. 但是,json输出存在错误。
First of all I have an Item class. 首先,我有一个Item类。
Item {
Id,
Name,
Sku,
Price,
Quantity
}
My cart class contains a List of Items 我的购物车类别包含商品清单
public List<Item> Items;
I'm using the following to serialize the list. 我正在使用以下序列化列表。
var jsonList = JavascriptSerializer.Serialize(cart.Items);
jsonList is then passed into javascript using Razor like so - 然后使用Razor将jsonList传递到javascript中-
<script type="text/javascript">
var items = @jsonList;
</script>
The result that is generated in the browser looks like this: 在浏览器中生成的结果如下所示:
items = [{"Id":ITEM_ID,"Name":"ITEM_NAME","Sku":"ITEM_SKU","Quantity":ITEM_QTY,"Price":ITEM_PRICE},{"Id":ITEM2_ID,"Name":"ITEM2_NAME",etc...}]
So I'd like to know how I get rid of the " and replace them with the required " instead. Does it have something to do with my Item class or my javascript? 因此,我想知道如何摆脱&quot;并将其替换为要求的&quot;。它与我的Item类或JavaScript有关系吗?
I've tried @Html.Raw(items) and no luck - returns an empty json object. 我已经尝试过@ Html.Raw(items),但没有运气-返回一个空的json对象。
您可以使用Html.Raw返回未编码的HTML
var items = @Html.Raw(@jsonList);
Found a solution that worked on the following post. 找到了适用于以下帖子的解决方案。
How do I write unencoded Json to my View using Razor? 如何使用Razor将未编码的Json写入视图?
Thanks to @James for mentioning unencoded HTML - that put me on the right path. 感谢@James提到了未编码的HTML-这使我走上了正确的道路。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.