我目前正在使用基本的CRUD操作制作一个小应用程序,我想在我的视图中显示所有书籍 . 但当我调用 .Books 对象时,它写道:

'IndexModel'不包含'Books'的定义,并且没有可访问的扩展方法'Books'可以找到接受类型'IndexModel'的第一个参数(你是否缺少using指令或汇编引用?)

这是我到目前为止所做的:

Book Model

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;

namespace CRUD_Razor_2_1.Model
{
    public class Book
    {
        public int Id { get; set; }

        [Required]
        public string Name { get; set; }

        public string ISBN { get; set; }

        public string Author { get; set; }
    }
}

ApplicationDbContext

using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace CRUD_Razor_2_1.Model
{
    public class ApplicationDbContext : DbContext
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
        {

        }

        public DbSet<Book> Books { get; set; }
    }
}

Index.html.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using CRUD_Razor_2_1.Model;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace CRUD_Razor_2_1.Pages.BookList
{
    public class CreateModel : PageModel
    {
        private readonly ApplicationDbContext _db;

        public CreateModel(ApplicationDbContext db)
        {
            _db = db;
        }
        [BindProperty]
        public Book Book { get; set; }


        public void OnGet()
        {

        }
        public async Task<IActionResult> OnPost()
        {
            if(!ModelState.IsValid)
            {
                return Page();
            }
            _db.Books.Add(Book);
            await _db.SaveChangesAsync();
            return RedirectToPage("Index");
        }
    }
}

在这里,当我想调用 Model.Book.Count() 时,我得到错误:

@page
@model IndexModel;


@{
}


<h2>Book List</h2>
<a asp-page="Create" class="btn btn-primary">Create New Book</a> @if(Model.Books.Count() > 0) { } else { }