Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asp.Net Core and Scaffold

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");    
        }    
    }    
}
like image 264
Rafael Avatar asked Sep 21 '17 17:09

Rafael


1 Answers

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))
like image 131
Kirk Larkin Avatar answered Sep 23 '22 22:09

Kirk Larkin