为什么我在执行 post 时收到 RazorPage 的错误?

Why am I receiving this error with RazorPage when performing a post?

我 运行 遇到了一些我似乎无法使用 RazorPages 并从 SQL 数据库填充 SelectList 的东西。我遵循了一个关于将数据绑定到 select 列表的教程,现在当我 post 我的表单出现以下错误时,它爆炸了:

InvalidOperationException: Could not create an instance of type 'Microsoft.AspNetCore.Mvc.Rendering.SelectList'. Model bound complex types must not be abstract or value types and must have a parameterless constructor. Alternatively, set the 'CarMakeList' property to a non-null value in the 'TestProject.Pages.IndexModel' constructor.

我有以下代码:

 public class IndexModel : PageModel
{
    private readonly IHostingEnvironment _hostingEnvironment;
    private readonly IVehicleService _vehicleService;
    private readonly ApplicationDbContext _context;


    public IndexModel(IHostingEnvironment hostingEnvironment, IVehicleService vehicleService, ApplicationDbContext context)
    {
        _hostingEnvironment = hostingEnvironment;
        _vehicleService = vehicleService;
        _context = context;

    }

    public Filters Filters { get; set; }

    public SelectList CarMakeList { get; set; }

    public IActionResult OnGet()
    {
        PopulateMakeDropdownList();
        return Page();
    }

    [BindProperty]
    public IFormFile Upload { get; set; }

    public async Task<IActionResult> OnPostAsync()
    {
        if (!ModelState.IsValid)
        {
            return Page();
        }

        var file = Path.Combine(_hostingEnvironment.WebRootPath, "uploads", Upload.FileName);
        using (var fileStream = new FileStream(file, FileMode.Create))
        {
            await Upload.CopyToAsync(fileStream);
        }

        HttpContext.Session.SetObject("CurrentFilters", Filters);

        return RedirectToPage("vehicles");
    }

    public void PopulateMakeDropdownList(object selectedMake = null)
    {
        var makeQuery = from m in _context.CarMake
            orderby m.Name
            select m;

        CarMakeList = new SelectList(makeQuery.AsNoTracking(), "MakeId", "Name", selectedMake);
    }

 public class CarMake
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    [Column("MakeId")]
    public int MakeId { get; set; }

    public string Name { get; set; }
}

这是我的过滤器class

public class Filters
{
    [Required]
    public int Source { get; set; }

    public int MmrPercentage { get; set; }

    public int MaximumPrice { get; set; }

    public int MinimumPrice { get; set; }

    public int Transport { get; set; }

    public int Recon { get; set; }

    public int ExtraCost { get; set; }

    public int MinimumOdometer { get; set; }

    public int MaximumOdometer { get; set; }

    public string InitialSortOption { get; set; }

    public string City { get; set; }

    public string State { get; set; }

    public string Make { get; set; }

    public string Model { get; set; }

    public int Year { get; set; }
}

我的前端是这样的:

 <div class="row">
    <div class="form-group">
        <label asp-for="Filters.Make">Vehicle Make:</label>
        <select id="makeDropDownList" asp-for="Filters.Make" class="form-control"
                asp-items="@Model.CarMakeList">
            <option value="-1">-- Select Make --</option>
        </select>
    </div>
</div>

我已经弄乱了大约一个小时,不知道为什么会这样。它在等待 next.Invoke()

的这段代码中的 startup.cs 文件中快要死了
 app.Use(async (context, next) =>
        {
            await next.Invoke();

            if (context.Response.StatusCode == StatusCodes.Status404NotFound)
            {
                context.Response.Redirect("/AndItsGone");
            }
        });

编辑:在我将 Make/Model select 列表添加到解决方案之前,一切都运行良好。页面加载正常,我一点击提交按钮就收到错误消息。

我知道这与 DI 有关,但我永远想不出来它是什么。我已经调整了我的方法并使用以下代码解决了问题:

我摆脱了 SelectList 与模型的绑定,现在正在这样做:

public IActionResult OnGet()
    {
        var makeQuery = from m in _context.CarMake
            orderby m.Name
            select m;

        ViewData["Makes"] = makeQuery.Select(n => new SelectListItem
            {
                Value = n.MakeId.ToString(),
                Text = n.Name
            }).ToList();

        return Page();
    }

以及前端代码:

<div class="row">
    <div class="form-group">
        <label asp-for="Filters.Make">Vehicle Make:</label>
        <select id="makeDropDownList" asp-for="Filters.Make" class="form-control"
                asp-items="@((List<SelectListItem>)ViewData["Makes"])">
            <option value="-1">-- Select Make --</option>
        </select>
    </div>
</div>