Transfer the data from the For loop to JavaScript

As I develop a survey form through the ASP.NET MVC structure, I encounter a phase where I extract the survey questions from a model to the view.

The model I have created is as follows:

[NotMapped]
    public class QuizForService
    {
        public int MainDetailsId { get; set; }
        public int QuizID { get; set; }
        public string Quiz { get; set; }
        public string AnswerForQuiz { get; set; }
    }

From the controller, I load the questions into this model:

List<QuizForService> Questionsto = (from q in db.tbl_QuestionsSub 
                                   where q.ServiceTypeId == ServiceId 
                                   select new QuizForService 
                                   {
                                    QuizID = q.Id,
                                    Quiz = q.QuestionEng
                                   }).ToList();

return Json(new {
  Success = true,
    questionlist = Questionsto,

}, JsonRequestBehavior.AllowGet);

In the view, I iterate through the questions using a loop and provide answer choices for users:

for (int i = 0; i < SurveyList.Count; i++) {
  <tr>
    <td> @SurveyList[i].Quiz </td>
    <td> @if(@SurveyList[i].QuizID != 8) {
      @SurveyList[i].AnswerForQuiz <div class="radio">
        <input label="Excellent πŸ˜€" type="radio" id="Excellent" name="@SurveyList[i].QuizID" value="Excellent" checked>
        <input label="Good πŸ™‚" type="radio" id="Good" name="@SurveyList[i].QuizID" value="Good">
        <input label="Fair 😐" type="radio" id="Fair" name="@SurveyList[i].QuizID" value="Fair">
        <input label="Poor 😢" type="radio" id="Poor" name="@SurveyList[i].QuizID" value="Poor">
        </div> } else { <div class="radio"> <
        input label="Yes πŸ˜€" type="radio" id="Yes" name="@SurveyList[i].QuizID" value="Yes" checked>
        <input label="No 😠" type="radio" id="No" name="@SurveyList[i].QuizID" value="No">
        <input label="Maybe πŸ€”" type="radio" id="Maybe" name="@SurveyList[i].QuizID" value="Maybe">
        </div> } </td>
        </tr>

This view serves as a single-page application that operates in a wizard-like fashion.

My plan involves using JavaScript to fetch the selected values and then pass them to the controller via Ajax.

I seek guidance on retrieving the question IDs along with the selected customer answers in JavaScript.

Answer β„–1

One way to utilize ajax is by incorporating the data-id attribute into your code. For instance;

<input label="PerhapsπŸ€”" type="radio" data-id="Perhaps" name="@SurveyList[i].QuizID" value="Perhaps">

Subsequently, you can employ ajax like so:

var idValue = $(this).data("Perhaps");

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

ability to reach the sub-element dictionaries in typescript

class ProvinciaComponent extends CatalogoGenerico implements OnInit, AfterViewInit { page: Page = new Page({sort: {field: 'description', dir: 'asc'}}); dataSource: ProvinciaDataSource; columns = ['codprovi ...

Instructions for adding a select dropdown feature in Angular 6 that includes a search filter. Additionally, tips on how to filter objects by their name property

I need to add an auto-complete feature in my Angular 6 app where the data is displayed as objects in a dropdown and filtered as we type. **template.html** <mat-form-field > <input matInput [matAutocomplete]="auto" [formControl]="customerFi ...

What is the best way to implement KaTeX in a React application?

Can someone help explain how KaTex can be incorporated into a React application? Despite reviewing the documentation on NPM, I am still having trouble understanding. I am specifically confused on how katex.render and katex.renderToString functions can be ...

Finding the actual path in any scenario using JavaScript ($.ajax)

Here is the structure of my project: Sil -> css -> js -> clients -> index.php -> response.php index.php I am facing an issue related to the folder 'clients'. My website URL can have different va ...

Javascript-generated HTML elements are invisible

I am attempting to create a "circle of fifths" using html, css, and javascript. I am following this tutorial: https://blog.logrocket.com/interactive-svg-circle-of-fifths/ Although I am using the astro framework, I don't believe my issue is related to ...

Mastering the flow of control in Node.js programs

Attempting to grasp control flow within Node.js applications. Specifically, does control loop back to the original function once the callback method completes, similar to a callback stack in recursive calls? A simple program was crafted to make a GET call ...

Using buttons as spinners for input elements with identical styles but unique identifiers

Currently, I am in the process of developing a project that involves users. In order to display all users, I am executing a query on an SQL database. After styling the interface, I have added an input element beside each user, which initializes at zero. Ad ...

I am unable to locate the appropriate TypeScript type for the `displayName` attribute when it is used as a prop for the `type` component

Having trouble finding the correct type as mentioned in the title. After inspecting with DevTools, I have confirmed that every component I am programmatically looking at has Component.type.displayName. For anything that is not an ElementType, the type is a ...

Experimenting with JavaScript code using npm without the need to host a website

As a part of my testing suite, I am seeking a way to execute JavaScript code without using a web browser and have the output displayed on the console or saved to a file. Is there a way to accomplish this? ...

Having difficulty sending emails with Nodemailer

This is a simple example showcasing the usage of Nodemailer library. var http = require('http'); var port = process.env.PORT || 8080; var async = require('async'); var nodemailer = require('nodemailer'); // Creating a transp ...

Anomalous Snackbar and Alert Interactions

Why am I getting an error with this code snippet: import Snackbar from "@mui/material/Snackbar"; import MuiAlert from "@mui/material/Alert"; const Alert = ({children}) => <MuiAlert>{children}</MuiAlert> <Snackbar ope ...

How to programmatically close a Bootstrap modal in a React-Redux application using jQuery

Hello everyone, I hope you're all doing well. I am currently working on a React application that utilizes Redux. I have run into an issue while trying to close a modal in Bootstrap programmatically. The versions I am using are Bootstrap 4 and jQuery 3 ...

Find the sum and subtotals for two different categories in a JavaScript array

Currently, I'm in the process of aggregating three totals: totalPoints, monthlyTotals, and monthlyByType by utilizing Array.prototype.reduce. So far, I've managed to successfully calculate totalPoints and monthlyTotals, but I'm encountering ...

What is the optimal amount of data that should be stored in a user's session object?

Our website utilizes a number of wizard-style form applications to gather user information, with each page capturing data that is then sent to a backend process via a web service. Due to the constraints of our system, we are unable to submit the informati ...

Struggling to target specific elements in CSS that are dynamically generated through JavaScript

After using JavaScript to generate some divs, I ended up with the following code: const container = document.querySelector('#container'); for(let i = 1; i < 17; i++) { var row = document.createElement('div'); row.id = 'r ...

Exploring the world of third-party widgets: Comparing Angular, jQuery, and traditional JavaScript

I have a plan to develop a simple embeddable widget similar to Google ads or Facebook like box. Users will be able to easily add the widget to their website and I will extract some parameters to fetch data from my servers. Previously, I relied on jQuery f ...

Unable to utilize jQuery's .append(data) function due to the need to use .val(append(data)) instead

I have been attempting to utilize JQuery .append(data) on success in order to change the value of an input to the appended data like this: .val(append(data)), but it doesn't seem to be working. Surprisingly, I can successfully change the value to a st ...

Retrieves MySQL SQL findings and converts them into a JSON structure

I'm in the process of integrating Typeahead.js into my website. The Typeahead.js plugin will retrieve data from a remote page that returns JSON, similar to: http://example.org/search?q=%QUERY Here's the PHP code I've written for my site: ...

What is the best way to keep my layout component fixed in the Next13 app directory?

I am struggling to develop a custom layout component that can retrieve its own data. Despite adding 'cache: 'force-cache'' to the fetch function, the updated content from my CMS is still being loaded every time I refresh the page. Below ...

How are the resources utilized by blocked threads?

Working with the asynchronous programming model, particularly using callbacks instead of blocking threads, serves the primary purpose of reducing the number of blocking threads within the system. The reason behind this objective is quite clear for running ...