在单独的下拉列表中显示保存值



i在编辑视图中具有一个来自数据库值的下拉列表。我要做的是在单独的下拉列表中显示保存值。例如,我用相同的外键保存了两个不同的数据,以确定这两个记录被视为一个记录。(请参阅下面的示例图像(

https://i.stack.imgur.com/yyvja.jpg

我仅使用单序列下拉列表,我只是在循环记录计数,以确定在"编辑"页面中显示多少个下拉列表。因此,如果我有"无伤害事件"one_answers"投诉"事件,则必须在单独的下拉列表中显示,因为我现在所做的是它们都在一个下拉列表中显示,因此结果是看来记录是重复的(请参阅下图(,但实际上,这两个记录在每个下拉列表中。

https://i.stack.imgur.com/z43rs.jpg
https://i.stack.imgur.com/xnlg7.jpg

查看

//for loop to count records that will determine how many dropdown list to be displayed
@for (var i = 0; i < Model.SavedEventsToList.Where(a => a.incidentReportId == Model.IRId).Count(); i++)
 {
    <tr>
       <td style="border-bottom:none !important;border-top:none !important;">
          <div class="input-group">  
              <select class="form-control pseEventDDLInEdit" id="pseEventListInEdit" name="pseAddedEvent">
                @{
                   foreach (var item in Model.SavedEventsToList)
                   {
                      if (item.selected == "yes")
                      {
                         if (item.incidentReportId == Model.IRId)   //this is the foreign key that determine these two records are as one
                         {
                            <option value=@item.pseEventsId selected>@item.pseEventsName</option>
                         }
                       }
                       else
                       {
                         <option value=@item.pseEventsId>@item.pseEventsName</option>
                       }                                                        
                    }
                  }
                </select>
            </div>
         </td>
     </tr>
  }

控制器

public ActionResult Edit(Guid? id)
{
  IMRBusinessLogic imrLogic = new IMRBusinessLogic();
  var imrRepo = new IMRRepository();
  IMRDTO imr = imrRepo.GetIRDetailsForEdit(id);
  imr.SavedEventsToList = imrLogic.SavedEvents(id);
   return View(imr);
}

public List<PSESavedEventsDTO> SavedEvents(Guid? incidentReportId)
{
  using (IREntities db = new IREntities())
  {
     var events = (from a in db.SavedPatientSafetyEvents(incidentReportId)
                select new PSESavedEventsDTO
                {
                  pseSavedEventId = a.pse_saved_event_category_and_subcategory_id,
                  pseEventsId = a.pse_events_id,
                  pseEventsName = a.pse_events_name,
                  seqNum = a.seq_num,
                  incidentReportId = a.incident_report_id,
                  savedRowIndex = a.saved_row_index,
                  selected = a.selected
                }).ToList();
       return events;
      }
}

我需要将它们分开,以便用户仍然可以选择编辑这两个记录中的每一个。

这是我需要的预期输出:https://i.stack.imgur.com/x9lcz.jpg

有人可以帮助我。预先感谢您。

我已经找到了解决方案。我只是使用foreach而不是循环,我得到了所需的输出。

@foreach (var index in Model.SavedEventsToList.Where(a => a.savedRowIndex != 0))
{
<tr>
  <td style="border-bottom:none !important;border-top:none !important;">
     <div class="input-group">
        <select class="form-control pseEventDDLInEdit" id="pseEventListInEdit" name="pseAddedEvent">
        @{
           foreach (var item in Model.SavedEventsToList)
           {
              if (item.selected == "yes")
              {
                 if (item.incidentReportId == Model.IRId && item.savedRowIndex == index.savedRowIndex)
                 {
                    <option value=@item.pseEventsId selected>@item.pseEventsName</option>
                 }
              }
              else
              {
                 <option value=@item.pseEventsId>@item.pseEventsName</option>
              }
           }
         }
        </select>
        <span title="Patient Safety Events Description" class="input-group-addon" data-toggle="popover" data-container="body" data-placement="right" data-trigger="hover" data-html="true" href="#" id="login"><i class="fa fa-info-circle"></i></span>
     </div>
  </td>
</tr>
}

最新更新