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))