I am trying to display the result from the database to the user on the front end but I keep getting the following exception:
InvalidCastException: Unable to cast object of type 'System.String' to type 'System.Int32'
and
System.InvalidOperationException: 'An exception occurred while reading a database value for property 'CList.CourseCategory'. The expected type was 'LearnAngebot.Models.Courscategory' but the actual value was of type 'System.String'.'
I am lost as to what has gone wrong.
I have a similar code to display the courses and it seems to be working fine. I used the same format for this but keep getting this error.
The C# code:
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 code:
@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>
}
Why exactly am i getting this exception and how do I resolve it? Thanks in advance.