问题
The generation of automatic scaffold to Asp.Net Razor Page is compatible to bool data types?
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
Its beautiful, but just working if my POCO class hasn't a bool type.
Example POCO class:
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' 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?)
Any idea?
Maybe is a necessary information the fact of I'm using SQLite as Database...
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");
}
}
}
回答1:
The problem is with this line:
@Html.DisplayNameFor(model => model.Active))
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))
回答2:
I raised this bug on the ASP.NET Core GitHub site and it was fixed in .NET Core 2.0: https://github.com/aspnet/Scaffolding/issues/830
However it doesn't look as if the fix was ported forward to 2.1 so I raised a new bug here: https://github.com/aspnet/Scaffolding/issues/855
来源:https://stackoverflow.com/questions/46349821/asp-net-core-and-scaffold