データベースからの結果をフロントエンドのユーザーに表示しようとしていますが、次の例外が引き続き発生します。
InvalidCastException: タイプ 'System.String' のオブジェクトをタイプ 'System.Int32' にキャストできません
と
System.InvalidOperationException: 'プロパティ 'CList.CourseCategory' のデータベース値の読み取り中に例外が発生しました。予想される型は「LearnAngebot.Models.Courscategory」でしたが、実際の値は「System.String」型でした。
何がうまくいかなかったのか途方に暮れています。
コースを表示するための同様のコードがあり、正常に機能しているようです。これには同じ形式を使用しましたが、このエラーが発生し続けます。
C# コード:
public class IndexModel : PageModel
{
public CUser User { get; set; }
public bool empty { get; set; }
public readonly IHostingEnvironment _env;
private readonly CDataContext _context;
private readonly IUserService _UserService;
public IndexModel(CFE_CrazyLabContext context, IUserService UserService, IHostingEnvironment hostingEnvironment)
{
_env = hostingEnvironment;
_context = context;
_UserService = UserService;
User = UserService.GetUser();
}
public IList<CList> ResultList { get; set; }
public CUser StudentUser { get; set; }
public CStudent Student { get; set; }
public void LoadList(CList list)
{
StudentUser = _UserService.GetUser(list.StudentUser);
}
public void OnGet()
{
ResultList = _context.Result.Where(o => EF.Functions.Like(o.StudentUser, User.UserName)).ToList();
}
}
HTML コード:
@if(Model.ResultList.Count == 0)
{
<div>
<h2> No Result yet </h2>
</div>
}
@if(Model.ResultList.Count > 0)
{
<div class="Custom-Table Custom-Table-Big">
<table>
<tbody>
<tr id="head">
<th><a href="index.pgp?">Student UserName</a></th>
<th><a href="index.php?">Course Name</a></th>
<th><a href="index.pgp?">Category</a></th>
<th><a href="index.pgp?">Date</a></th>
</tr>
@foreach(var item in Model.ResultList)
{
Model.LoadList(item);
<tr>
<td>
@item.StudentUser
</td>
<td>
@item.CourseName
</td>
<td>
@item.CourseCategory
</td>
<td>
@item.Date
</td>
</tr>
}
</tbody>
</table>
</div>
}
なぜこの例外が発生するのですか?どうすれば解決できますか? 前もって感謝します。