7
votes

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");    
        }    
    }    
}
2
how does the CreateModel class look like ? You added here the Movie class.Andrei Neagu
The example can be downloaded at: github.com/aspnet/Docs/blob/master/aspnetcore/tutorials/… , but is necessary change the DbContext to SQLite and one property of POCO to boolRafael
Is there anywhere else in the code you reference Active other than the code you've shown? This seems to be a compilation issue so where exactly is it appearing?serpent5
It appears in webpage, when I click at Create link, which was created by scaffold.Rafael

2 Answers

13
votes

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))
1
votes

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