Showing data on the webpage using ASP.NET Core MVC

Here is the task at hand: The goal is to show information that was previously entered by the user on an editing page, with the data coming from a database. The requirement is to display existing information, allow for additions or deletions of current entries. Below is the code snippet: controller: This code snippet retrieves all professions associated with the user (and it works)

foreach(var up in _context.TutorProfessionAndCosts.ToList())
         {
             if (user.Id == up.UserId)
                 user.ProfessionAndCosts.Add(up);
         }

profession model

public class TutorProfessionAndCost
 {
     public int Id { get; set; }

     public string UserId { get; set; }
     public User User { get; set; }
     public int IdTutorProfession { get; set; }
     public string EducationalSubject { get; set; }
    
     public int Cost_One { get; set; } //the cost of the lesson is individual
     public int Cost_Group { get; set; } //cost of class group class

     public string Currency { get; set; }
     public int IdCurrency { get; set; }
 }

editing page (currently not displaying previously entered information):

<div id="ProfessionBlock">
        
         <div class="Profession">
             <div class="row">
                 <div class="col">
                     <p class="mb-0">Choose the subject you teach:</p>

                     <input type="hidden" name="ProfessionAndCosts[0].UserId" value="@Model.Tutor.Id" />
                     <select name="ProfessionAndCosts[0].EducationalSubject" required="required">
                         @foreach (var profession in Model.educational_Subject)
                         {
                             <option value="@profession.EducationalSubject" >@profession.EducationalSubject</option>
                         }
                     </select>


                 </div>
                 <div class="col">
                     <p>Indicate the price for 1 hour:</p>
                     <div>
                         <p>Individual class: <input class="money" type="text" name="ProfessionAndCosts[0].Cost_One" /> </p>
                         <p>Group class: <input class="money" type="text" name="ProfessionAndCosts[0].Cost_Group" /> </p>
                         <label>Choose a currency: </label>
                         <select name="ProfessionAndCosts[0].Currency">
                             @foreach (var cur in Model.currency)
                             {
                                 <option value="@cur.CurrencyMoney">@cur.CurrencyMoney</option>
                             }
                         </select>
                     </div>
                 </div>
             </div>
         </div>
        
        
     </div>
     <p>
         <a class="addNewProfession">Add new profession</a>
     </p>

Javascript function to add a new profession (working):

@section Scripts {
 <script>
     $(function () {
         var i = 0;
         $('.addNewProfession').click(function () {
             i++;
             var html2Add = `<div class="row">
         <div class="col">
             <p class="mb-0">Choose the subject you teach:</p>
             <select name="ProfessionAndCosts[${i}].EducationalSubject" required="required">
                 @foreach (var profession in Model.educational_Subject)
                 {
                     <option value="@profession.EducationalSubject">@profession.EducationalSubject</option>
                 }
             </select>
         </div>
         <div class="col">
             <label>Indicate the cost for 1 hour:</label>
             <p>
                 <p>Individual class: <input class="money" type="text" name="ProfessionAndCosts[${i}].Cost_One" /> </p>
                 <p>Group class: <input class="money" type="text" name="ProfessionAndCosts[${i}].Cost_Group" /> </p>
                 <select name="ProfessionAndCosts[${i}].Currency">
                     @foreach(var cur in Model.currency)
                     {
                         <option value="@cur.CurrencyMoney">@cur.CurrencyMoney</option>
                     }
                 </select>
             </p>
         </div>
     </div>`;
             $('#ProfessionBlock').append(html2Add);
         })
     })
 </script>

Attempted display code which allows editing but not adding new information:

@for (int p = 0; p < Model.Tutor.ProfessionAndCosts.Count(); p++)
 {
     <div class="Profession">
         <div class="row">
             <div class="col">
                 <p class="mb-0">Choose the subject you teach:</p>

                 <input type="hidden" name="ProfessionAndCosts[p].UserId" value="@Model.Tutor.Id" />
                 <select name="ProfessionAndCosts[p].EducationalSubject" required="required">
                    @foreach (var profession in Model.educational_Subject)
                     {
                         <option value="@profession.EducationalSubject" selected="@(profession.EducationalSubject == Model.Tutor.ProfessionAndCosts[p].EducationalSubject)">profession.EducationalSubject</option>
                     }
                 </select>


             </div>
             <div class="col">
                 <p>Indicate the price for 1 hour:</p>
                 <div>
                     <p>Individual lesson: <input class="money" type="text" name="ProfessionAndCosts[p].Cost_One" value="@Model.Tutor.ProfessionAndCosts[p].Cost_One" /> </p>
                     <p>Group lesson: <input class="money" type="text" name="ProfessionAndCosts[p].Cost_Group" value="@Model.Tutor.ProfessionAndCosts[p].Cost_Group" /> </p>
                     <label>Choose a currency: </label>
                     <select name="ProfessionAndCosts[p].Currency">
                         @foreach (var cur in Model.currency)
                         {
                             <option value="@cur.CurrencyMoney" selected="@(cur.CurrencyMoney == Model.Tutor.ProfessionAndCosts[p].Currency)">@cur.CurrencyMoney</option>
                         }
                     </select>
                 </div>
             </div>
         </div>
     </div>

Answer №1

The issue was resolved by moving the

<div class="Profesion">

<div class="Profession">
             @for (int p = 0; p < Model.Tutor.ProfessionAndCosts.Count(); p++)
             {
                 <div class="row">
                     <div class="col">
                         <p class="mb-0">Select the subject you are teaching:</p>

                         <input type="hidden" name="ProfessionAndCosts[@p].UserId" value="@Model.Tutor.Id" />
                         <select name="ProfessionAndCosts[@p].EducationalSubject" required="required">
                             @foreach (var profession in Model.educational_Subject)
                             {
                                 <option value="@profesion.EducationalSubject" selected="@(profesion.EducationalSubject == Model.Tutor.ProfessionAndCosts[@p].EducationalSubject)">@profesion.EducationalSubject</option>
                             }
                         </select>


                     </div>
                     <div class="col">
                         <p>Specify the price for one hour:</p>
                         <div>
                             <p>Individual session: <input class="money" type="text" name="ProfessionAndCosts[@p].Cost_One" value="@Model.Tutor.ProfessionAndCosts[@p].Cost_One" /> </p >
                             <p>Group session: <input class="money" type="text" name="ProfessionAndCosts[@p].Cost_Group" value="@Model.Tutor.ProfessionAndCosts[p].Cost_Group" /> </p>
                             <label>Choose a currency: </label>
                             <select name="ProfessionAndCosts[@p].Currency">
                                 @foreach (var cur in Model.currency)
                                 {
                                     <option value="@cur.CurrencyMoney" selected="@(cur.CurrencyMoney == Model.Tutor.ProfessionAndCosts[@p].Currency)">@cur.CurrencyMoney</option>
                                 }
                             </select>
                         </div>
                     </div>
                 </div>
                 <hr />
             }
             </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

`Adding data to Rails database and displaying without the need for reloading using AngularJS`

When working on my Rails project, I encountered an issue with adding data to the database using `http.post` from an AngularJS controller. Below is the code snippet demonstrating this: RestaurantIndexCtrl.js.coffee: restauranteur.controller 'Restaura ...

Tips for ensuring a controller function only runs once in AngularJS

I have a controller that is being referenced in some HTML. The HTML changes on certain events, causing the controller function code to execute multiple times. The issue lies in a portion of the code that should only be executed once. Shown below is the ...

How can I retrieve multiple rows from a SQL Server stored procedure in a C# ASP.NET application?

I have encountered an issue while trying to call a SQL Server stored procedure that returns multiple values based on a given ID. For example, if you input ID = 1, it should return the names of all people with ID = 1. My objective is to store the IDs in a ...

Executing ts-node scripts that leverage imported CSS modules

Is there a way to execute scripts that utilize css modules? I am currently working on a typescript migration script that I would like to execute using ts-node. The ideal scenario would be to keep the script's dependencies separate from the React comp ...

Analyzing exif data during the process of uploading a batch of images

My website allows users to upload multiple images simultaneously. I want to check the EXIF rotation tag of each uploaded image in order to manually rotate the images in the browser if needed. I came across a solution for reading the EXIF rotation value of ...

Selecting the optimal data structure: weighing the benefits of using boolean check versus array .include (balancing performance and redundancy

My objects can have one or more properties assigned, with a total of 5 different properties in my case. To illustrate this, let's use a simple movie example where each movie can be assigned from 5 different genres. I have come up with two methods to ...

Having trouble with loading image textures in three.js

Here is the code snippet I am using: var scene = new THREE.Scene(); // adding a camera var camera = new THREE.PerspectiveCamera(fov,window.innerWidth/window.innerHeight, 1, 2000); //camera.target = new THREE.Vector3(0, 0, 0); // setting up the renderer ...

Encountering an issue with displaying data fetched from omdbapi, receiving [object Object] instead

Everything is working perfectly in my code except for the mysterious appearance of [object Object] whenever I perform a search. I've combed through my code multiple times, but I just can't seem to locate the source of this issue. It would be grea ...

The OnsenUi getCurrentPage() function returns an empty object

As I work on creating a hybrid mobile app using Onsen UI, I've encountered an issue regarding navigation and data transfer between two pages. My starting point is index.html, which consists of a main.html ons-template along with ons-navigation and ons ...

The function for the "load next page" action in ngInfiniteScroll is continuously triggered

After attempting to implement infinite scrolling using the "loading remote data" example from the ngInfiniteScroll website, I am facing a problem. The function nextPage() is being called continuously until all records have been loaded (controlled by an of ...

Encountering an issue with Firebase authentication in Next.js: the window object is not defined

Here is some code snippets: import { initializeApp } from "firebase/app"; import { getAnalytics } from "firebase/analytics"; import { getAuth } from "firebase/auth"; const firebaseConfig = { //credentials// }; export const ...

Obtain an Instagram access token with the help of Node.js

I've been attempting to acquire an access token from Instagram without utilizing a browser request, using the instagram-node module. However, I haven't been successful in receiving the access token. Can anyone provide me with a solution that does ...

How can I generate a list of separate words from innerText using JavaScript?

Wondering how to parse a string correctly using JavaScript? Take a look at the example below: <strong>word</strong>: or <em>word</em> or <p><strong>word</strong>: this is a sentence</p> etc... The objective ...

Scrollmagic - Creating dynamic effects by animating body and div backgrounds using multiple tweens

I'm currently developing a website that utilizes scrollmagic to smoothly transition the color of the active div (each set to size of the screen) from black back to white as you scroll down. The issue I am facing is that the body background color does ...

Navigate to a specific assignment labelled as "foo"

My approach to this problem involves using divs with specific titles. <div id="foo"></div> <div id="foo2"></div> <div id="foo3"></div> <div id="foo4"></div> <a class ...

Creating a jQuery Mobile Multi-Select feature with dynamically loaded data

I'm facing an issue with integrating jQuery Mobile and AngularJS. I need a multi-select option for users to choose categories, which have parent-child relationships. The data structure is fetched asynchronously from a database. The problem is that t ...

In search of a jQuery parser for HTML strings that can accurately parse <a> links and <img> images

Is there a HTML string jQuery parser available, specifically for parsing <a> links and <img> images? Alternatively, I am looking for code that can extract all links and images from a HTML string, which can potentially be very large. For exampl ...

In the realm of asp.net, the OnClick event springs into action after the

My asp button uses OnClientClick to execute a javascript function, and I also want to run OnClick after OnClientClick. However, the OnClick never seems to execute. Despite looking through similar topics on StackOverflow and other websites, I can't see ...

Utilizing JavaScript to Display JSON Content on an HTML Page

I am hoping to achieve a layout similar to this: Image related to the output My question is, how can I generate this output without repeatedly including <p> using just pure JavaScript and JSON, without relying on any JavaScript libraries? The JSON ...

Explore the fetch JSON API for efficient searching

After successfully fetching data from my API using React and obtaining JSON in my console, I am now faced with a new challenge. I have a search component that I want to use to search within the JSON data I retrieved and display the results on the screen. ...