Asp.Net核心和脚手架

编程入门 行业动态 更新时间:2024-10-22 23:42:47
本文介绍了Asp.Net核心和脚手架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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

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

I ask about it, because I'm following this tutorial: docs.microsoft/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:

'CreateModel'不包含'Active'的定义,并且找不到扩展方法'Active'接受类型为'CreateModel'的第一个参数(您是否缺少using指令或程序集引用?)

'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作为数据库的事实也许是必要的信息...

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核心和脚手架

本文发布于:2023-11-17 00:03:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1608063.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:脚手架   核心   Asp   Net

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!