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 PagesMovies --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; } } } 有了这个实现,我会在尝试创建一个电影时得到这个错误:
任何的想法? 也许是我使用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)) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |