0

The task is as follows: I need to display the information previously entered by the user on the editing page (the information is taken from the database). It is necessary that the existing information is displayed, it would be possible to add a new one or delete an existing one. I have the following code: controller: with the help of this code I get all the professions that the user has (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 (does not display 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 code that allows you to add a new profession (it works)

@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>

}

I tried to display the information as follows, but in such a situation, the program allows you to "edit" the existing information, but does not add new

@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>

1 Answer 1

0

The problem was solved by putting "<div class="Profesion">" outside the loop:

<div class="Profession">
             @for (int p = 0; p < Model.Tutor.ProfessionAndCosts.Count(); p++)
             {
                 <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="@profesion.EducationalSubject" selected="@(profesion.EducationalSubject == Model.Tutor.ProfessionAndCosts[@p].EducationalSubject)">@profesion.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>
                 <hr />
             }
             </div>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.