【问题标题】:How to display database information on webpage using Razor-pages如何使用 Razor-pages 在网页上显示数据库信息
【发布时间】:2022-01-12 17:42:44
【问题描述】:

This is the image of database I'm working with

我想显示“单词”,但只使用“id”。如果我有 id “11111”,我需要显示单词“abascus”

这是我到目前为止所做的:

//This is the code behind file (.cshtml.cs)
public IList<testdiceware> testdiceware { get; set; }
public async Task OnGetAsync()
{
    testdiceware = await _db.testdiceware.ToListAsync();
}

这就是我试图显示信息的方式

//This is the .cshtml where I'd like to display the result
@Html.DisplayFor(model => model.testdiceware[11111].word);

这给了我 ArgumentOutOfBounds 异常可能是因为它引用了这个value (marked in red),但我想显示与“id”值对应的“word”

我面临的主要障碍是:

我正在生成数字“11111”,我需要一种仅使用生成的数字“11111”来显示相应单词的方法,有没有办法实现?

【问题讨论】:

    标签: c# asp.net asp.net-core razor razor-pages


    【解决方案1】:

    对于testdiceware[index].word[]中的值应该是list testdiceware的索引,不能将11111放入其中。当参数的值超出允许范围时抛出异常ArgumentOutOfBounds调用方法定义的值。您可以尝试使用

    @Html.DisplayFor(model => model.testdiceware[0].word);
    

    获取第一个testdiceware的消息。

    更新:

    你可以尝试使用:

    @Model.testdiceware.Where(t => t.id == 11111).ToList()[0].word
    

    【讨论】:

    • 是的,我明白为什么它超出了界限,但限制是我正在生成数字“11111”,我需要一种方法来仅使用生成的数字“11111”来显示相应的单词,有没有办法实现以下?
    • 你好,我已经更新了关于找到对应的带有id的单词的答案。
    • 嘿,这行得通!非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2019-12-22
    • 1970-01-01
    • 2014-05-24
    • 2021-08-28
    • 1970-01-01
    • 2010-09-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多