InvalidOperationException:不存在具有键 'GenreId' 的 'IEnumerable<SelectListItem>' 类型的 ViewData 项

InvalidOperationException: There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'GenreId'

我有这个代码。我看过其他堆栈溢出帖子,但对我没有任何作用。我想念什么吗?我需要为流派、出版商和作者制作一些下拉列表,但我找不到任何可以帮助我的东西。我尝试了 viewbag,因为我在很多地方都找到了它,但它对我不起作用。我不知道 viewbag 是否适用于 .net mvc core 3.1

图书模型

 public class Book
{
    public int BookId { get; set; }

    public string Title { get; set; }

    public int AvailableCopies { get; set; }

    public int TotalCopies { get; set; }

    public string GenreId { get; set; }

    public int PublisherId { get; set; }

    public Genre Genre { get; set; }

    public Publisher Publisher { get; set; }

    public ICollection<BookAuthor> BookAuthors { get; set; }

    public ICollection<BorrowProcess> BorrowProcesses { get; set; }
}

图书管理员:

 //POST: Books/Create
    [HttpPost]
    [ValidateAntiForgeryToken]
    public IActionResult Create([Bind("BookId,Title,TotalCopies,AvailableCopies")] Book book)
    {

        if (ModelState.IsValid)
        {

            ViewBag.GenreId = new SelectList(_genres, "GenreId", "Name");
            _bookService.AddBook(book);
            _bookService.Save();
            return RedirectToAction(nameof(Index));
        }
        return View(book);

    }

创建视图:

@model LibraryApplication.Models.Book

@{
    ViewData["Title"] = "Create";
}

<h1>Create</h1>

<h4>Book</h4>
<hr />
<div class="row">
    <div class="col-md-4">
        <form asp-action="Create">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <div class="form-group">
                <label asp-for="Title"></label>
                <input asp-for="Title" class="form-control" />
                <span asp-validation-for="Title" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="AvailableCopies"></label>
                <input asp-for="AvailableCopies" class="form-control" />
                <span asp-validation-for="AvailableCopies" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="TotalCopies"></label>
                <input asp-for="TotalCopies" class="form-control" />
                <span asp-validation-for="TotalCopies" class="text-danger"></span>
            </div>
            @*<div class="form-group">
                <label asp-for="GenreId"></label>
                <select asp-for="GenreId" class="form-control" asp-items="ViewBag.GenreId"></select>
            </div>*@
        <div class="form-group">
            @Html.DropDownList("GenreId", null, new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.GenreId)
        </div>
            <div class="form-group">
                <label asp-for="PublisherId"></label>
                <select asp-for="PublisherId" class="form-control" asp-items="ViewBag.PublisherId"></select>
            </div>
            <div class="form-group">
                <input type="submit" value="Create" class="btn btn-primary" />
            </div>
        </form>
    </div>
</div>

<div>
    <a asp-action="Index">Back to List</a>
</div>

我正在使用 MVC Core 3.1。可能是什么问题?

更新:

public BooksController(BookService bookService, AuthorService authorService, GenreService genreService, PublisherService publisherService)
 {
 _bookService = bookService;
 _authorService = authorService; 
_genreService = genreService;
 _publisherService = publisherService;
 _genres = _genreService.GetGenres(); } 
// GET: Books 
public IActionResult Index(string SearchString) 
{ 
var books = _bookService.GetBooks();
 return View(books); 
} 
.....

修复下拉列表。试试这个

 @Html.DropDownListFor(model => model.GenreId, @ViewBag.GenreId, "Choose an option", new { @class = "form-control" })

你还需要一个动作

     [HttpGet]
     public IActionResult   Create()
    {

            ViewBag.GenreId = new SelectList(_genres, "GenreId", "Name");
            
           var model = new Book();
        return View(model);

    }