Error message: Property ID not found - JSON data retrieved from a C# ASP.NET List<>

I've been working on this issue for a few days now with no progress.

My goal is to convert a List object into JSON for use with Google Analytics' e-commerce service, requiring it to be passed to JavaScript first.

However, I'm encountering an error with the JSON output.

Initially, I have defined an Item class.

Item {
    Id,
    Name,
    Sku,
    Price,
    Quantity
}

Within my cart class, there is a List of Items.

public List<Item> Items;

To serialize the list, I am using the following code.

var jsonList = JavascriptSerializer.Serialize(cart.Items);

The resulting jsonList is then passed to JavaScript using Razor as follows -

<script type="text/javascript">
    var items = @jsonList;
</script>

After rendering in the browser, the generated result appears like this:

items = [{&quot;Id&quot;:ITEM_ID,&quot;Name&quot;:&quot;ITEM_NAME&quot;,&quot;Sku&quot;:&quot;ITEM_SKU&quot;,&quot;Quantity&quot;:ITEM_QTY,&quot;Price&quot;:ITEM_PRICE},{&quot;Id&quot;:ITEM2_ID,&quot;Name&quot;:&quot;ITEM2_NAME&quot;,etc...}]

I am seeking advice on how to remove the &quot; and replace them with quotation marks instead. Could the issue lie within my Item class or my JavaScript?

I have attempted to use @Html.Raw(items) without success - it returns an empty JSON object.

Answer №1

To display unencoded HTML, consider utilizing the Html.Raw method

var data = @Html.Raw(@jsonData);

Answer №2

Discovered a successful solution in the post below.

How can I output unencoded JSON in my Razor View?

Big thanks to @James for pointing out unencoded HTML - it really helped me figure things out.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Dealing with notifications using setInterval

I am currently working on creating a Facebook-style notification using setInterval. The notification is functioning correctly, but the issue I am facing is that the div containing the notification closes every 6 seconds. I suspect the root cause is the set ...

Clearly defining the schema as a JSON document for seamless integration between Spark and MongoDB

When combining spark and mongodb, it is feasible to offer a sample schema in the form of an object - as explained on: https://docs.mongodb.com/spark-connector/master/scala/datasets-and-sql/#sql-declare-schema For convenience, there is example code demonst ...

Getting data from a JSON column in SQL Server that begins with an array element can be achieved by using the OPEN

I am facing a challenge with extracting data from a database table where the column of interest contains JSON formatted data. The complexity arises from the fact that the outermost elements of the JSON structure are '[' and ']', indicat ...

Error encountered in Node/Express application: EJS partials used with Angular, causing Uncaught ReferenceError: angular is not defined

I seem to be missing something important here as I attempt to incorporate ejs partials into a single-page Angular app. Every time I try, I encounter an Uncaught ReferenceError: angular is not defined in my partial. It seems like using ejs partials instead ...

Transmit information securely to a web address using a button

I have a scenario where I have 3 buttons, and upon clicking any of them, I want to send the values within the <li></li> tags to a page called send.php. For instance, if I click on the first button, I wish to secretly send the value 1 to the s ...

Exploring CouchDB through Ajax to interact with a static website

Is it feasible for my HTML static website to interact with CouchDB using AJAX and retrieve the data without relying on server-side languages like PHP or Python? The CouchDB might be hosted on a different server/domain, so JSONP would need to be utilized. ...

Building multiple files in React allows developers to divide their code

Can a React/Redux application be split into modules during webpack build? For example, having a set of components for users and another set for invoices. I want webpack to generate users.js and invoices.js that can be imported into index.html. If I make ch ...

What could be causing my JavaScript to malfunction after clicking on anchor links?

My website's JavaScript has a bug where clicking the links in the static header causes scrolling to become buggy and unbearable. What can I do to fix this issue? Thank you in advance. Here is my code: $(window).load(function(){ $(windo ...

The perfect combination of NUnit Unit Tests with TestContainers leads to achieving worldwide acclaim for their `OneTimeSetup` and `OneTimeTearDown

I am currently working on utilizing TestContainers with NUnit for running my .NET tests. I have a situation where two test cases start, interact with a created MongoDb container, perform their tasks, and then dispose of the container. The screenshot below ...

Modify Border Color of Textbox upon Entering Text

Need help with a checkbox and a nearby textbox. When the checkbox is checked, the border color of the textbox should turn green. If the checkbox is unchecked, the border color of the textbox should be red. Jquery code: $('#item_availability1_unlimite ...

AngularJS Class Confirmation Button

I'm currently working on implementing a "confirm" button for users of my website to see after clicking a specific button, using an angularJS class. Below is the code snippet I have written: class TodosListCtrl { constructor($scope, $window){ $s ...

Top method for handling chained ajax requests with jQuery

I'm facing a scenario where I have to make 5 ajax calls. The second call should only be made once the first call returns a response, the third call after the second completes, and so on for the fourth and fifth calls. There are two approaches that I ...

Troubleshooting Issue: MVC 5 validation messages not displaying when using Ajax.BeginForm

Having recently delved into MVC 5, I've encountered some issues that have left me stumped despite my best efforts at troubleshooting. Specifically, my struggle lies in creating a validation mechanism for a user and password list to ensure that all fie ...

The integration of PHP code with an HTML form is causing issues

When I use the insert_teacher('bla bla','bla bla','dqsd') function in a PHP file, everything works fine. However, when I try to implement it with an HTML form, nothing is displayed and no data is inserted into my database. &l ...

What steps do I need to take in order to make fullscreen slides?

Seeking assistance in developing full-screen slides similar to those found on the following website... The browser scrollbar should be hidden, and the slides should automatically transition when scrolling up/down or using the up/down arrow keys, with the a ...

NetCore offers an alternate method for generating serializers using XmlSerializer

In our programming, we employ the XmlSerializer to create temporary assemblies that contain serializers. These assemblies are then cached to keep resource usage at a minimum. Currently, our application operates on the .NET Framework, and the code snippet ...

Unexpected behavior encountered with JQueryUI modal functionality

Today marks my first experience with JqueryUI. I am attempting to display a conditional modal to notify the user. Within my ajax call, I have this code snippet: .done(function (result) { $('#reportData').append(result); ...

The absence of the Django CSRF token has been detected

Inside the custom.js file, there is a function defined as shown below : function contactTraxio(fullname, telephone, email) { if (typeof(fullname)==='undefined') fullname = null; if (typeof(telephone)==='undefined') telephone = ...

Endless loading with Restangular

Currently, I am using a Restangular collection which is functioning perfectly: $scope.threads = Restangular.all('thread').getList({ limit: 10, offset: 0 }).$object; However, my goal is to enhance the functionality by implementing a feature wher ...

How to pass arguments to page.evaluate (puppeteer) within pkg compiled applications

When I run my puppeteer script directly with node, everything works fine. However, once I compile the source using pkg, the page.evaluate and page.waitForFunction functions start failing with a SyntaxError: Unexpected identifier error. The specific code ...