Asp.Net 核心和支架
Asp.Net Core and Scaffold
生成自动脚手架到Asp.Net Razor 页面是否兼容 bool 数据类型?
我问一下,因为我正在学习这个教程:https://docs.microsoft.com/en-us/aspnet/core/tutorials/razor-pages/model。在创建 POCO class、配置 dbContext 和迁移之后,我 运行 此命令自动生成脚手架
dotnet aspnet-codegenerator razorpage -m Movie -dc MovieContext -udl -outDir Pages\Movies --referenceScriptLibraries
它很漂亮,但如果我的 POCO class 没有 bool 类型,它就可以工作。
示例 POCO class:
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 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
中,在使用 this 的地方,model
指的是 CreateModel
而不是 Movie
。相反,您需要:
@Html.DisplayNameFor(model => model.Movie.Active))
我在 ASP.NET Core GitHub 网站上提出了这个错误,它已在 .NET Core 2.0 中修复:
https://github.com/aspnet/Scaffolding/issues/830
但是看起来修复程序并没有移植到 2.1,所以我在这里提出了一个新错误:
https://github.com/aspnet/Scaffolding/issues/855
生成自动脚手架到Asp.Net Razor 页面是否兼容 bool 数据类型?
我问一下,因为我正在学习这个教程:https://docs.microsoft.com/en-us/aspnet/core/tutorials/razor-pages/model。在创建 POCO class、配置 dbContext 和迁移之后,我 运行 此命令自动生成脚手架
dotnet aspnet-codegenerator razorpage -m Movie -dc MovieContext -udl -outDir Pages\Movies --referenceScriptLibraries
它很漂亮,但如果我的 POCO class 没有 bool 类型,它就可以工作。
示例 POCO class:
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 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
中,在使用 this 的地方,model
指的是 CreateModel
而不是 Movie
。相反,您需要:
@Html.DisplayNameFor(model => model.Movie.Active))
我在 ASP.NET Core GitHub 网站上提出了这个错误,它已在 .NET Core 2.0 中修复: https://github.com/aspnet/Scaffolding/issues/830
但是看起来修复程序并没有移植到 2.1,所以我在这里提出了一个新错误: https://github.com/aspnet/Scaffolding/issues/855