
実現したいこと
スキャフォールディングで自動生成されたページが正常に機能しません。
前提
ユーザーの追加機能を作成しています。
UserInfoモデルを元に新規スキャフォールディングを用いてAccountフォルダ内に生成されたCRUDファイルを使用しております。
([新しいスキャフォールディングの追加] >[Entity Framework を使用する Razor Pages (CRUD)]で作成)
発生している問題・エラーメッセージ
InvalidOperationException: Could not create an instance of type 'FunctionList.UserInfo'. 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 'UserInfo' property to a non-null value in the 'FunctionList.Pages.Account.CreateModel' constructor.
該当のソースコード
UserInfo.cs
1namespace FunctionList 2{ 3 public class UserInfo 4 { 5 public string id { get; set; } 6 public string password { get; set; } 7 public string name { get; set; } 8 public UserInfo(string id, string password, string name) 9 { 10 this.id = id; 11 this.password = password; 12 this.name = name; 13 } 14 15 } 16} 17
pages/Account/Create.cshtml.cs
1using System; 2using System.Collections.Generic; 3using System.Linq; 4using System.Threading.Tasks; 5using Microsoft.AspNetCore.Mvc; 6using Microsoft.AspNetCore.Mvc.RazorPages; 7using Microsoft.AspNetCore.Mvc.Rendering; 8using FunctionList; 9using FunctionList.Data; 10 11namespace FunctionList.Pages.Account 12{ 13 public class CreateModel : PageModel 14 { 15 private readonly FunctionList.Data.FunctionListContext _context; 16 17 public CreateModel(FunctionList.Data.FunctionListContext context) 18 { 19 _context = context; 20 } 21 22 public IActionResult OnGet() 23 { 24 return Page(); 25 } 26 27 [BindProperty] 28 public UserInfo UserInfo { get; set; } 29 30 31 // To protect from overposting attacks, see https://aka.ms/RazorPagesCRUD 32 public async Task<IActionResult> OnPostAsync() 33 { 34 if (!ModelState.IsValid) 35 { 36 return Page(); 37 } 38 39 _context.UserInfo.Add(UserInfo); 40 await _context.SaveChangesAsync(); 41 42 return RedirectToPage("./Account/index"); 43 } 44 } 45} 46
pages/Account/Create.cshtml
1@page 2@model FunctionList.Pages.Account.CreateModel 3 4@{ 5 ViewData["Title"] = "Create"; 6} 7 8<h1>Create</h1> 9 10<h4>UserInfo</h4> 11<hr /> 12<div class="row"> 13 <div class="col-md-4"> 14 <form method="post"> 15 <div asp-validation-summary="ModelOnly" class="text-danger"></div> 16 <div class="form-group"> 17 <label asp-for="UserInfo.id" class="control-label"></label> 18 <input asp-for="UserInfo.id" class="form-control" /> 19 <span asp-validation-for="UserInfo.id" class="text-danger"></span> 20 </div> 21 <div class="form-group"> 22 <label asp-for="UserInfo.password" class="control-label"></label> 23 <input asp-for="UserInfo.password" class="form-control" /> 24 <span asp-validation-for="UserInfo.password" class="text-danger"></span> 25 </div> 26 <div class="form-group"> 27 <label asp-for="UserInfo.name" class="control-label"></label> 28 <input asp-for="UserInfo.name" class="form-control" /> 29 <span asp-validation-for="UserInfo.name" class="text-danger"></span> 30 </div> 31 <div class="form-group"> 32 <input type="submit" value="Create" class="btn btn-primary" /> 33 </div> 34 </form> 35 </div> 36</div> 37 38<div> 39 <a asp-page="Index">Back to List</a> 40</div> 41 42@section Scripts { 43 @{await Html.RenderPartialAsync("_ValidationScriptsPartial");} 44} 45
試したこと
下記を参考に自分のソースを確認しましたがどこが間違っているか分かりませんでした。。。
https://stackoverflow.com/questions/45194022/di-exception-in-asp-net-core-could-not-create-an-instance-of-type-myproject-da
補足情報(FW/ツールのバージョンなど)
環境
OS:Windows11
フレームワーク:.NET 6.0
ASP.NET Core Razor page




