向Asp.Net Razor Page自动生成支架是否兼容bool数据类型?
The generation of automatic scaffold to Asp.Net Razor Page is compatible to bool data types?
我问这个问题,因为我正在关注本教程: docs.microsoft/zh-CN/aspnet/core/tutorials/razor-pages/model .在创建POCO类,配置dbContext和迁移之后,我运行了此命令以自动生成支架
I ask about it, because I'm following this tutorial: docs.microsoft/en-us/aspnet/core/tutorials/razor-pages/model. And at a point, after create the POCO class, configure dbContext and migrations, I ran this command to automatic generate the scaffold
dotnet aspnet-codegenerator razorpage -m Movie -dc MovieContext -udl -outDir Pages\Movies --referenceScriptLibraries它很漂亮,但是如果我的POCO类不是bool类型,就可以工作.
Its beautiful, but just working if my POCO class hasn't a bool type.
示例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; } } }通过此实现,当尝试创建电影时会出现以下错误:
With this implementation I'll get, when try to Create a Movie, this error:
'CreateModel'不包含'Active'的定义,并且找不到扩展方法'Active'接受类型为'CreateModel'的第一个参数(您是否缺少using指令或程序集引用?)
'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作为数据库的事实也许是必要的信息...
Maybe is a necessary information the fact of I'm using SQLite as Database...
和CreateModel类:
And the CreateModel class:
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 中, model 指的是 CreateModel 而不是 Movie .相反,您需要:
In Create.cshtml, where this is used, model refers to a CreateModel rather than a Movie. Instead, you need:
@Html.DisplayNameFor(model => model.Movie.Active))更多推荐
Asp.Net核心和脚手架
发布评论