本文介绍了Asp.Net核心和脚手架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The generation of automatic scaffold to Asp.Net Razor Page is compatible to bool data types?

我问这个问题,因为我正在关注本教程: https://docs.microsoft.com/zh-CN/aspnet/core/tutorials/razor-pages/model .在创建POCO类,配置dbContext和迁移之后,我运行了此命令以自动生成支架

I ask about it, because I'm following this tutorial: https://docs.microsoft.com/en-us/aspnet/core/tutorials/razor-pages/model. And at a point, after create the POCO class, configure dbContext and migrations, I ran this command to automatic generate the scaffold

dotnet aspnet-codegenerator razorpage -m Movie -dc MovieContext -udl -outDir Pages\Movies --referenceScriptLibraries

它很漂亮,但是如果我的POCO类不是bool类型,就可以工作.

Its beautiful, but just working if my POCO class hasn't a bool type.

示例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; }
    }
}

通过此实现,当尝试创建电影时会出现以下错误:

With this implementation I'll get, when try to Create a Movie, this error:

有什么主意吗?

我正在使用SQLite作为数据库的事实也许是必要的信息...

Maybe is a necessary information the fact of I'm using SQLite as Database...

和CreateModel类:

And the 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 中, model 指的是 CreateModel 而不是 Movie .相反,您需要:

In Create.cshtml, where this is used, model refers to a CreateModel rather than a Movie. Instead, you need:

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

这篇关于Asp.Net核心和脚手架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 21:56
查看更多