Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BindProperty attribute should support primary constructors with classes #58205

Open
1 task done
christiannagel opened this issue Oct 2, 2024 · 0 comments
Open
1 task done
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates

Comments

@christiannagel
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe the problem.

In an ASP.NET Core Razor project, creating this class with a primary constructor

public class Book(string title, string? publisher = default, int id = 0)
{
    public int Id { get; set; } = id;

    [StringLength(50)]
    public string Title { get; set; } = title;

    [StringLength(50)]
    public string? Publisher { get; set; } = publisher;
}

succeeds in scaffolding creating Razor pages with EF Core.

This is the code from the Create page code-behind:

public class CreateModel : PageModel
{
    private readonly RazorPagesWithEFCore.Data.BooksContext _context;

    public CreateModel(RazorPagesWithEFCore.Data.BooksContext context)
    {
        _context = context;
    }

    public IActionResult OnGet()
    {
        return Page();
    }

    [BindProperty]
    public Book Book { get; set; } = default!;

Running the application fails with an InvalidOperationException:

InvalidOperationException: Could not create an instance of type 'RazorPagesWithEFCore.Models.Book'. Model bound complex types must not be abstract or value types and must have a parameterless constructor. Record types must have a single primary constructor. Alternatively, set the 'Book' property to a non-null value in the 'RazorPagesWithEFCore.CreateModel' constructor.

Describe the solution you'd like

Using a parameterless constructor as described with the error, the application runs. I would prefer to use the primary constructor. Using records, the error mentions it's ok using a primary constructor. This should be possible with a class as well (and scaffolding creates code successfully).

Additional context

The alternative option mentioned in the error message "Alternatively, set the 'Book' property to a non-null value in the 'RazorPagesWithEFCore.CreateModel' constructor.", is not working. Assigning a non-null value to the Book property fails as well.

@dotnet-issue-labeler dotnet-issue-labeler bot added the area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates label Oct 2, 2024
@christiannagel christiannagel changed the title BindProperty fails with primary constructors with classes BindProperty should support primary constructors with classes Oct 2, 2024
@christiannagel christiannagel changed the title BindProperty should support primary constructors with classes BindProperty attribute should support primary constructors with classes Oct 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates
Projects
None yet
Development

No branches or pull requests

1 participant