gpt4 book ai didi

c# - Asp.Net Core 和脚手架

转载 作者:太空狗 更新时间:2023-10-29 20:02:27 26 4
gpt4 key购买 nike

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com