排查 ASP.NET Core MVC 中枚举值下拉显示的问题

问题描述 投票:0回答:2

我想在主管批准后允许用户下载报告。目前,我正在使用经理帐户,他可以在其中检查许多报告并将其状态更改为已验证或拒绝,但我不明白为什么报告状态枚举列表未显示,即使它显示在控制台。

HTML code

型号:

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>
c# model-view-controller asp.net-core-mvc
2个回答
1
投票

问题是因为我最后遗漏了这段代码。

<script>
$(document).ready(function () {
    $('select').formSelect();
});
</script>

-1
投票

以下是修改代码的方法:

  1. 确保在视图文件顶部导入必要的命名空间:
@using Microsoft.AspNetCore.Mvc.Rendering
  1. 修改控制器操作以使用枚举值正确填充 ViewBag:
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();
}
  1. 更新您的视图以将下拉列表正确绑定到枚举值:

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

通过这些更改,您的下拉列表应正确填充枚举值,并且每个报告的状态应相应地显示在表中。

© www.soinside.com 2019 - 2024. All rights reserved.