c# – Asp.Net Core和Scaffold

Asp.Net Razor Page的自动脚手架的生成是否与bool数据类型兼容?

我问它,因为我正在学习本教程:https://docs.microsoft.com/en-us/aspnet/core/tutorials/razor-pages/model.在创建POCO类之后,配置dbContext和迁移,我运行此命令以自动生成脚手架

dotnet aspnet-codegenerator razorpage -m Movie -dc MovieContext -udl -outDir Pages\Movies --referenceScriptLibraries

它的美丽,但只是工作,如果我的POCO类没有bool类型.

示例POCO类:

using System;

namespace RazorPagesMovie.Models
{
    public class Movie
    {
        public int ID { get; set; }
        public string Title { get; set; }
        public DateTime ReleaseDate { get; set; }
        public string Genre { get; set; }
        public bool Active { get; set; }
    }
}

有了这个实现,我会在尝试创建一个电影时得到这个错误:

‘CreateModel’ does not contain a definition for ‘Active’ and no extension method ‘Active’ accepting a first argument of type ‘CreateModel’ could be found (are you missing a using directive or an assembly reference?)

任何的想法?

也许是我使用SQLite作为数据库这一事实的必要信息……

而CreateModel类:

using System;    
using System.Collections.Generic;    
using System.Linq;    
using System.Threading.Tasks;    
using Microsoft.AspNetCore.Mvc;    
using Microsoft.AspNetCore.Mvc.RazorPages;    
using Microsoft.AspNetCore.Mvc.Rendering;    
using RazorPagesMovie.Models;

namespace RazorPagesMovie.Pages.Movies    
{    
    public class CreateModel : PageModel    
    {    
        private readonly RazorPagesMovie.Models.MovieContext _context;    

        public CreateModel(RazorPagesMovie.Models.MovieContext context)    
        {    
            _context = context;    
        }        

        public IActionResult OnGet()    
        {    
            Movie = new Movie    
            {    
                Title = "The Good,the bad,and the ugly",Genre = "Western",Price = 1.19M,ReleaseDate = DateTime.Now,Active = true    
            };    
            return Page();    
        }        

        [BindProperty]    
        public Movie Movie { get; set; }    

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

            _context.Movie.Add(Movie);    
            await _context.SaveChangesAsync();    

            return RedirectToPage("./Index");    
        }    
    }    
}

解决方法

问题出在这一行:
@Html.DisplayNameFor(model => model.Active))

在Create.cshtml中,使用它时,模型引用CreateModel而不是Movie.相反,你需要:

@Html.DisplayNameFor(model => model.Movie.Active))

相关文章

本文将从上往下,循序渐进的介绍一系列相关.NET的概念,先从...
基于 .NET 的一个全新的、好用的 PHP SDK + Runtime: Pe...
.NET 异步工作原理介绍。
引子 .NET 6 开始初步引入 PGO。PGO 即 Profile Guided Opti...
前言 2021/4/8 .NET 6 Preview 3 发布,这个版本的改进大多来...
前言 开头防杠:.NET 的基础库、语言、运行时团队从来都是相...