Transform an Asp.net core page modal object into a Vue.js data object

Combining Asp.netcore Razor Pages with Vue.js is my goal for a new project. The page modal will return the necessary data, which the Vue controller will then utilize for client-side rendering and other UI interactions.

Pagemodal.cs

   public class SampleQuestionModel : PageModel
    {
        public List<QuestionViewModal> questionViewModals = new List<QuestionViewModal>();
        public void OnGet()
        {
            MYEXAMAPI.Controllers.QuestionApiController questionApiController = new MYEXAMAPI.Controllers.QuestionApiController();

            questionViewModals = questionApiController.GetQuestion();
        }
    }

This is how I showcase the data using Razor:

@page
@model MYEXAM.SampleQuestionModel
@{
    ViewData["Title"] = "SampleQuestion";
}

<h1>SampleQuestion</h1>
<script src="~/js/vue.js"></script>

<ul id="app">
    <li v-for="item in questionViewModals">
        {{ item.fullQuestions }}
    </li>
</ul>

<script>

    var vueApp = new Vue({
        el: '#app',
        data: {
            questionViewModals: @Json.Serialize(Model.questionViewModals) //This is not working 
        }
    })
</script>

Answer ā„–1

Check out this functional code snippet for reference:

@page
@model RazorPages3_0Test.SampleQuestionModel

<h1>SampleQuestion</h1>

<ul id="app">
  <li v-for="item in questionViewModals">
    {{ item.fullQuestions }}
  </li>
</ul>

@section Scripts
{
  <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
  <script>
    var vueApp = new Vue({
      el: '#app',
      data: {
        questionViewModals: @Json.Serialize(Model.questionViewModals) 
      }
   })
  </script>
 }

Outcome

https://i.sstatic.net/wtFia.png

If you need to download Vue.js, you can get it from here

Answer ā„–2

If you're not familiar with ASP.net, consider moving the

@Json.Serialize(Model.questionViewModals)
code snippet to the created hook.

<script>

var vueApp = new Vue({
    el: '#app',
    created() {
        this.questionViewModals = @Json.Serialize(Model.questionViewModals);
    },
    data: {
        questionViewModals: null
    }
})
</script>

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

Wordpress functionality for filtering Ajax posts using radio buttons

I am in the process of creating an Ajax post filter system using radio buttons to allow users to filter through multiple categories. Below is the code I have implemented: Front-end form: <form id="filter"> <?php if( ...

An undocumented finished status in Node.js

Currently diving into the world of node.js with the guidance of Node Cookbook. However, I've hit a roadblock when it comes to URL routing. Let's take a look at the code snippet provided in the book: var http = require('http'); var pat ...

Ways to create the initial row text in jqgrid treegrid appear bold

Trying to make the text in the first row of a jqgrid treegrid bold? Here's what I attempted: #tree-grid .cell-wrapper { font-weight: bold; } However, this makes all nodes bold. How can I make only the first node (first row) bold? The treegrid i ...

Rendering JSON maps from MongoDB using Node.js

I am looking to display data from a JSON map in my Express application Here is my code: var b = det.map(function(h) { var soalid = h.id_soal; var idarray = [ ]; for (var i = 0; i < soalid.length; i++) { Soal.findOne({ ...

Tips for personalizing the css styles of an alert box?

I am in need of customizing the alert box using CSS attributes. Currently, I am able to achieve a similar effect with the code below: JQUERY: $('<div class="alertMessage">Error first</div>') .insertAfter($('#componentName' ...

Ways to adjust the text size in jqGrid?

The current theme is ui-lightness. How can I adjust the font size within the grid? Your suggestions are appreciated. Many thanks. ...

Vue fails to receive data from Axios

I am having trouble loading a cytoscape graph using vue and axios. I attempted to configure cytoscape, but encountered scope issues. So, I decided to try using axios and vue only. However, the problem persists, and I'm not sure what needs to be change ...

Are the single quote ('') and double quote (") characters used in jQuery the same way as they are in PHP?

Is there a function for either the single quote (') or double quote (") that will parse through a string and replace variables with values? I recall in PHP, the parsing engine would automatically switch out variables with their values (I don't re ...

How can I establish a breakpoint on an HTML element?

For instance, I have a button element with the click event attached to an ID in XXX.js file (the actual file name is unknown), and there are several .js files in the project. How can I debug the button click if I don't know where the function for it i ...

Loop through the numbers entered into a table field

<table id="Container1Details" cellspacing="15"> <tbody> <tr> <td align="right"> <input name="acidity" type="number" maxlength="4" id="acidity" min="0.112" max="0.152" step="0.001" style= ...

Execute the JavaScript function window.print() with a designated URL provided

In my asp.net mvc 2.0, I have created a report in the View which includes a header, footer, and content from searching for each record. The goal is to provide users with a preview of what they want to print before allowing them to click on a link or button ...

Discover tweets containing particular hashtags and display them on a webpage

During my search, I stumbled upon a helpful post by user gnab explaining how to locate a hashtag. If you want to find tweets with a specific hashtag, you can easily retrieve them by visiting . This will provide you with a JSON list of tweets that incl ...

MVC constructs the HTML page but fails to display it

I am facing an issue with Asp.net MVC. On my Index.cshtml page, I have the following Javascript code: $('#employeeTable tbody').on('dblclick', 'tr', function() { var employeeId = table.row(this).data().Id; ...

What is the best way to iterate through and hide these images when they are clicked on?

I need help creating a grid of images inside divs that function like checkboxes. When you click on an image, it should disappear, and clicking again should make it reappear. I have some code that works for one image, but I can't figure out how to loop ...

Direct your attention to the cursor problem in Internet Explorer

Here is a snippet of code that automatically changes background images: function changeBackground() { currentBackground++; if(currentBackground > 3) currentBackground = 0; $('body').fadeOut(0, function() { $('body&ap ...

I'm looking to enhance my code by adding a new user login password. How can I achieve this?

Currently, I am in the process of adding another user and have been exploring various code snippets available on different websites. I decided to test the following example, which worked perfectly. However, I am now wondering how I can add an additional u ...

Creating JSON files in JavaScript - Issue with adding data

My goal is to generate a unique JSON dataset structured in the following way: JSON: { "category": { "id": "10100", "name": "Dessert", "products": [ ...

Using JavaScript to create an HTML table, where the first cell in the first row appears as 'undefined' without a value

My function below generates a table in a forloop with ids for each tag. I want to calculate the total of each item (quantity * price). The function works fine after the first row, but the total of the first row returns as undefined, possibly because the v ...

Developing single-page application frontends without utilizing node or npm

As a backend java developer with experience in spring boot, I am now exploring the world of single page applications. One aspect that appeals to me about SPA frameworks, particularly Vue, is things like model-binding and the use of components and template ...

Using VueJs to dynamically adjust the width of an element based on another element

Iā€™m currently developing a Trivia app that features an <h1> element which dynamically changes every 10 seconds. My goal is to align the width of a radio button group component (b-form-group from Bootstrap-Vue components) with the width of the <h ...