我想在主管批准后允许用户下载报告。目前,我正在使用经理帐户,他可以在其中检查许多报告并将其状态更改为已验证或拒绝,但我不明白为什么报告状态枚举列表未显示,即使它显示在控制台。
型号:
public class Report
{
public int ID { get; set; }
[Display(Name = "Report Name")]
public string reportName { get; set; }
public virtual User reportManager { get; set; }
[Display(Name = "State")]
public ReportState reportState { get; set; }
public byte[] reportData { get; set; }
}
public enum ReportState
{
Accepted,
Pending,
Denied
}
控制器:
public async Task<IActionResult> Index()
{
ViewBag.Reports = await _context.Reports.ToListAsync();
ViewBag.ReportStates = new SelectList(Enum.GetNames(typeof(ReportState)));
return View();
}
@model variable_pay_system.Models.Report
@{
ViewData["Title"] = "Reports";
}
<div class="container">
<h5>Reports</h5>
<table>
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.reportName)
</th>
<th>
@Html.DisplayNameFor(model => model.reportState)
</th>
</tr>
</thead>
<tbody>
@foreach (Report report in ViewBag.Reports)
{
<tr>
<td>
@Html.DisplayFor(modelItem => report.reportName)
</td>
<td>
<select asp-for="@report.reportState" asp-items="Html.GetEnumSelectList<ReportState>()"></select>
</td>
</tr>
}
</tbody>
</table>
问题是因为我最后遗漏了这段代码。
<script>
$(document).ready(function () {
$('select').formSelect();
});
</script>
以下是修改代码的方法:
@using Microsoft.AspNetCore.Mvc.Rendering
public async Task<IActionResult> Index()
{
var reports = await _context.Reports.ToListAsync();
ViewBag.Reports = reports;
// Populate ViewBag with enum values
ViewBag.ReportStates = new SelectList(Enum.GetValues(typeof(ReportState)));
return View();
}
@model variable_pay_system.Models.Report
@{
ViewData["Title"] = "Reports";
}
<div class="container">
<h5>Reports</h5>
<table>
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.reportName)
</th>
<th>
@Html.DisplayNameFor(model => model.reportState)
</th>
</tr>
</thead>
<tbody>
@foreach (Report report in ViewBag.Reports)
{
<tr>
<td>
@Html.DisplayFor(modelItem => report.reportName)
</td>
<td>
<select asp-for="@report.reportState" asp-items="@(ViewBag.ReportStates as SelectList)"></select>
</td>
</tr>
}
</tbody>
</table>
</div>
通过这些更改,您的下拉列表应正确填充枚举值,并且每个报告的状态应相应地显示在表中。