我在编辑视图中有一个下拉列表,该列表具有数据库中的值。我要做的是在单独的下拉列表中显示保存的值。例如,我已经用相同的外键在数据库中保存了两个不同的数据,以确定这两个记录被视为一个。 (请参见下面的示例图片)
我仅使用单选下拉列表,并且仅循环记录的数量,以确定要在编辑页面中显示多少个下拉列表。因此,如果我有“无害事件”和“投诉”事件,则此事件必须显示在单独的下拉列表中,因为我现在所做的是它们都显示在一个下拉列表中,因此结果看起来像是重复记录(请参见下图),但实际上这两个记录都在每个下拉列表中。
https://imgur.com/YlVZHWx
https://imgur.com/FXYO4Tn
查看
//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://imgur.com/uwVjvkz
有人可以帮我吗? 预先谢谢你。
答案 0 :(得分:0)
我已经找到了解决方案。我只是使用foreach而不是for循环,然后得到所需的输出。
@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>
}