c# - Asp.Net Core 和脚手架

标签 c# asp.net razor asp.net-core scaffolding

Asp.Net Razor Page 的自动脚手架生成是否兼容 bool 数据类型?

我问这个问题,因为我正在学习本教程:https://learn.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 中,使用 this 的地方,model 指的是 CreateModel 而不是 Movie .相反,您需要:

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

关于c# - Asp.Net Core 和脚手架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46349821/

相关文章:

c# - 清除事件的优雅方式

c# - 如何检查是否按下了屏幕键盘 Ctrl 键?

c# - Asp.net mvc Url.Action 使用可为空参数重定向 Controller

c# - 如何为 IEnumerable<dynamic> 赋值

c# - ASP.Net MVC 中错误模型的编译时错误

c# - 我们可以创建批处理文件来对来自 TFS 服务器的构建进行排队吗?

c# - 如何开始使用 Microsoft Bot Framework?

asp.net - jquery UI 对话框和 Asp.net UpdatePanel

c# - 命名空间中不存在类型或命名空间名称 'DirectoryServices'?

c# - 为什么 Visual Studio 在 Razor (.cshtml/.razor) 中突出显示双花括号?