Utilizing ASP.NET Core MVC, I have implemented a dynamic display of content through bootstrap cards featuring navigation tabs. These cards

Is it possible to display data from a database in ASP.NET Core MVC using a foreach loop to generate different bootstrap cards with navigation tabs?

I have attempted this, and it seems to work partially. However, when I click on a navigation tab in the second card, the tabs switch only on the first card.

This is my index.html:

<div class="row">

@foreach (var q in Model.Question)
{
    <div class="col-sm-12 col-lg-6 pt-sm-4 pt-4">
        <div class="card ">
            <div class="card-header">
                <ul class="nav nav-tabs card-header-tabs" id="fragen-list" role="tablist">
                    <li class="nav-item">
                        <a class="nav-link active" href="#frage" role="tab" aria-controls="frage" aria-selected="true">Frage</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="#antwort" role="tab" aria-controls="antwort" aria-selected="false">Antwort</a>
                    </li>
                </ul>
            </div>
            <div class="card-body">
                <div class="tab-content mt-3 mt-sm-3">
                    <div class="tab-pane active" id="frage" role="tabpanel">
                        <h5 class="card-title text-center">@q.Question</h5>
                    </div>


                    <div class="tab-pane" id="antwort" role="tabpanel" aria-labelledby="history-tab">
                        <p class="card-text">@Model.Answer.Single(a => a.Id == q.AnswerId).Antwort1</p>
                        <ul>
                            @if (@Model.Answer.Single(a => a.Id == q.AnswerId).Example1 != null)
                            {
                                <li class="card-text">@Model.Answer.Single(a => a.Id == q.AnswerId).Example1</li>
                            }
                            @if (@Model.Answer.Single(a => a.Id == q.AnswerId).Example2 != null)
                            {
                                <li class="card-text">@Model.Answer.Single(a => a.Id == q.AnswerId).Example2</li>
                            }
                            @if (@Model.Answer.Single(a => a.Id == q.AnswerId).Example3 != null)
                            {
                                <li class="card-text">@Model.Answer.Single(a => a.Id == q.AnswerId).Example3</li>
                            }
                            @if (@Model.Answer.Single(a => a.Id == q.AnswerId).Example4 != null)
                            {
                                <li class="card-text">@Model.Answer.Single(a => a.Id == q.AnswerId).Example4</li>
                            }

                        </ul>

                        @if (@Model.Answer.Single(a => a.Id == q.AnswerId).Antwort2 != null)
                        {
                            <p class="card-text">@Model.Answer.Single(a => a.Id == q.AnswerId).Antwort2</p>
                        }
                        @if (@Model.Answer.Single(a => a.Id == q.AnswerId).Antwort3 != null)
                        {
                            <p class="card-text">@Model.Answer.Single(a => a.Id == q.AnswerId).Antwort3</p>
                        }
                    </div>
                </div>
            </div>
            <div class="card-footer text-center">
                <p class="card-text small text-black-50">Ausbildungsfach: @q.Category &nbsp; | &nbsp; Prüfungsfach: @q.ExamCategory &nbsp; | &nbsp; Schulfach: @q.SubjectCategory </p>
            </div>
        </div>
    </div>
}
</div>

This is my simple javascript file:

$('#fragen-list a').on('click', function (e) {
e.preventDefault()
$(this).tab('show')

});

Card Nav Header:

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

When I click on "Answer" in card 2, the content on card 1 changes to "Answer" instead of card 2

Answer №1

Revise your code as demonstrated below:

<div class="row">
    @{var i = 0; }     //include this...
    @foreach (var q in Model.Question)
    {
        <div class="col-sm-12 col-lg-6 pt-sm-4 pt-4">
            <div class="card ">
                <div class="card-header">
                    <ul class="nav nav-tabs card-header-tabs" id="fragen-list" role="tablist">
                        <li class="nav-item">               //modify this...
                            <a class="nav-link active" href="#frage_@i" role="tab" aria-controls="frage" aria-selected="true">Frage</a>
                        </li>
                        <li class="nav-item">         //update here...
                            <a class="nav-link" href="#antwort_@i" role="tab" aria-controls="antwort" aria-selected="false">Antwort</a>
                        </li>
                    </ul>
                </div>
                <div class="card-body">
                    <div class="tab-content mt-3 mt-sm-3">
                                                        //adjust here...
                        <div class="tab-pane active" id="frage_@i" role="tabpanel">
                            <h5 class="card-title text-center">@q.Question</h5>
                        </div>

                                                  //update here...
                        <div class="tab-pane" id="antwort_@i" role="tabpanel" aria-labelledby="history-tab">                                                                  
                              //...     
                        </div>
                    </div>
                </div>
                //...
            </div>
        </div>
        i++;       //include this...
    }
</div>

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

Could you assist me in navigating the process of creating a dynamic 10x10 multiplication table using HTML and JavaScript? I'm eager to learn where to begin with this methodology

As I explore various questions related to the "simple" multiplication tables, I find myself with a more fundamental query. I am seeking clarity on how Javascript operates when intertwined with HTML, as that is where my confusion lies. In my multiplication ...

`Stop the JW Player from playing several videos simultaneously`

I have a situation where I want to display multiple videos on the same page, but only one should be playable at a time. When a new video is started, the currently playing video should automatically stop. <div class="tab-pane active" id="abc"><d ...

retrieving multiple values in ajax call and processing them in controller function

I'm facing a challenge in Laravel when it comes to sending multiple input options (generated by a foreach loop) through an ajax call. The goal is to send multiple values and loop through them in the ajax call to effectively pass them to the controller ...

CSS for when the mouse hovers over an element

Two php files and a js file are involved in this scenario. The issue at hand is that when the div is clicked, it navigates to a new page with an appended # to the URL, causing it to scroll to the specified comment ID. However, instead of scrolling to the c ...

Typescript fails to recognize the imported variable within a generic type declaration

I am in the process of developing a versatile repository that can be used for every entity within the application. mongo-repository.ts import { Document, Model, Types } from 'mongoose'; type MongooseModel<T> = Model<T & Document&g ...

What sets apart the two syntaxes for JavaScript closures?

Similar Query: Is there a distinction between (function() {…}()); and (function() {…})();? I've come across a way to prevent variables from becoming global by using the following syntax: (function ($, undefined) { })(jQuery); Rec ...

Ways to verify if the user has inputted a typeahed value

My code snippet looks like this: var students = new Bloodhound({ datumTokenizer: Bloodhound.tokenizers.obj.whitespace('fullName'), queryTokenizer: Bloodhound.tokenizers.whitespace, remote: { ...

Switching between a secondary menu using ClassieJS or jQuery?

Currently, the code is configured to toggle the same menu for every icon. To see my current progress, you can check out this fiddle: http://jsfiddle.net/2Lyttauv/ My goal is to have a unique menu for each individual icon. I began by creating the followi ...

In JavaScript, it is not possible to retrieve the maximum or minimum value of an array

Hey there! I'm having some trouble finding the minimum and maximum values in an array. Can you help me figure out what's going on with my code? var repeated, studentsArray = [], marksArray = []; while (repeated !== 'n' && r ...

Guide to shutting down a print dialogue in a web browser with javascript

Looking for a way to close the print window of a browser using JavaScript or any other method, with JavaScript being the preferred option. Any help on closing the print window for IE, Chrome and Safari would be greatly appreciated. Please assist.. Thank ...

A unique issue arises when custom geometry is registered in A-Frame but fails to render once added to a scene

I have developed custom functions to create non-hollow THREE.js geometries with phi length values that are less than 360. Now, I want to display them in an A-Frame scene (embedded within React). However, I am facing issues while trying to render them. Ini ...

Determine the Total of a Column in jqgrid

Hey there, I'm looking for a way to calculate the total sum of the "montant" column in my jqgrid and then display it below the grid as "Total: *total amount*". Here is the code for my grid: <sjg:grid id="gridtable" caption="Quittance Payé ...

What is the best way to create a collage of images with a random and unique arrangement?

Recently, I stumbled upon a website and was intrigued by the unique effect of images randomly appearing on the screen. I'm curious about how this effect can be achieved. Is it possible to use CSS Grid to divide the screen and designate some grid ite ...

Finding the precise top offset position with jQuery upon scrolling – a step-by-step guide

I need help with detecting the offset top of a TR element when it is clicked. It works fine initially, but once the page is scrolled, the offset().top value changes. How can I resolve this issue? $(function() { $('tr').click(function() { ...

Utilize the Webpack library and libraryTarget settings to establish our own custom library as a global variable

I currently have a library named "xyz" that is being imported as a node module from the npm registry. Now, I want to incorporate it as a library and make it accessible under the global name "abc". To achieve this, I plan to utilize webpack configuration. ...

The navigation is designed to only show up as I scroll down the page, but ideally it should be visible

I am trying to make the navigation bar appear on page load instead of when I scroll down the page. Currently, I am using this jQuery code: <script type="text/javascript> $(document).scroll(function() { if ($(this).scrollTop() == 20) { ...

The Material UI export of 'useInsertionEffect' (imported as 'useInsertionEffect$1') was not located within the 'react' library while utilizing the Appbar component

https://i.sstatic.net/twJwh.png 1: https://i.sstatic.net/Y2Zdo.png**strong text**https://i.sstatic.net/twJwh.png While attempting to implement the Appbar component from Material UI, I encountered an unexpected error. ...

Discover the ultimate guide to creating an interactive website using solely JavaScript, without relying on any server-side

I'm facing a challenge: I have a desire to create a website that is mainly static but also includes some dynamic components, such as a small news blog. Unfortunately, my web server only supports static files and operates as a public dropbox directory. ...

Utilizing CSS transitions to smoothly adjust the width of an element until it completely fills the container div in ReactJS

import React from 'react'; import PropTypes from 'prop-types'; import SearchIcon from '@material-ui/icons/Search'; import InputBase from '@material-ui/core/InputBase'; import { AccountCircle } from '@material-ui ...

Regular expressions are used for validation purposes

I've been using the regular expression shown below to validate certain text: ^\d+(?:fs|sf)[-+]\d+[hmd]$/ Here are some sample texts I have validated with this regular expression: 20fs-4d 10sf+20m 3fs-2h So far, it's been working we ...