asp.net 在页面上显示信息, www.example.com 核心mvc

mnemlml8  于 2023-05-02  发布在  .NET
关注(0)|答案(1)|浏览(162)

任务如下:我需要在编辑页面上显示用户以前输入的信息(信息取自数据库)。显示现有信息是必要的,可以添加新信息或删除现有信息。我有以下代码:控制器:在这段代码的帮助下,我得到了用户拥有的所有职业(它可以工作)

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

职业模式

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; }
 }

编辑页面(不显示以前输入的信息):

<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代码,允许您添加一个新的职业(它的作品)

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

}
我试着如下显示信息,但在这样的情况下,程序允许你“编辑”现有的信息,但不添加新的

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

koaltpgm1#

这个问题可以通过将“<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">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>

相关问题