2017-09-21 218 views
2

Asp.Net Razor Page的自动脚手架的生成与bool数据类型兼容吗?Asp.Net Core和Scaffold

我问了一下,因为我按照这个教程: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类有没有布尔类型。

例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");  
     }  
    }  
} 
+0

CreateModel类是怎么样的?你在这里添加了Movie类。 –

+0

@AndreiNeagu我更新了CreateModel类的帖子.. – Rafael

+0

该示例可以从以下网址下载:https://github.com/aspnet/Docs/blob/master/aspnetcore/tutorials/razor-pages/razor-pages-start/sample/RazorPagesMovie /,但必须将DbContext更改为SQLite,并将POCO的一个属性更改为bool – Rafael

回答

4

问题是这一行:

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

Create.cshtml中,在使用这个的情况下,model是指CreateModel而不是Movie。相反,你需要:

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

也许这是脚手架程序可能的改进,它不是创造的事实,布尔属性,电影部分? – Rafael

+0

Yup脚手架有一个错误。 – empty