【问题标题】:InvalidCastException: Unable to cast object of type 'System.String' to type 'System.Int32'InvalidCastException:无法将“System.String”类型的对象转换为“System.Int32”类型
【发布时间】:2019-12-24 11:15:41
【问题描述】:

我试图在前端向用户显示数据库中的结果,但我不断收到以下异常:

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>
}

我究竟为什么会收到此异常,我该如何解决?提前致谢。

【问题讨论】:

  • 不应该是 item 而不是 @item inside loop?

标签: c# .net-core razor-pages inner-exception


【解决方案1】:

请检查数据库日期数据类型和模型日期数据类型并匹配。如果您在任何其他属性中遇到错误,请使用 F11 进行调试并匹配数据库数据类型和模型数据类型。

【讨论】:

  • 欢迎.. 哈哈哈开发者日期很混乱:)
【解决方案2】:

InvalidCastException:无法将“System.String”类型的对象转换为“System.Int32”类型:此错误源于表(模型)中的列/字段之一的数据类型错误

我会建议您仔细查看您的模型,并确保您用于保存数据的数据类型类型,因为系统不会为您固定指向特定字段或列。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-03
    • 1970-01-01
    • 2013-11-04
    • 2021-10-25
    相关资源
    最近更新 更多