简体   繁体   English

如何使用 Razor-pages 在网页上显示数据库信息

[英]How to display database information on webpage using Razor-pages

This is the image of database I'm working with这是我正在使用的数据库的图像

I want to display the "word" but using only the "id".我想显示“单词”,但只使用“id”。 If I have the id "11111", I need to display the word "abascus"如果我有 id “11111”,我需要显示单词“abascus”

This is what I have done so far:这是我到目前为止所做的:

//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 how I'm trying to display information这就是我试图显示信息的方式

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

This gives me ArgumentOutOfBounds exception probably cause it's referring to this value (marked in red) , but I want to display the "word" corresponding to the "id" value这给了我 ArgumentOutOfBounds 异常可能是因为它引用了这个值(标记为红色) ,但我想显示对应于“id”值的“word”

The main hurdle I'm facing is:我面临的主要障碍是:

I'm generating the number '11111' and I need a way to display the corresponding word only using the generated number '11111', is there a way to achieve that?我正在生成数字“11111”,我需要一种方法来仅使用生成的数字“11111”来显示相应的单词,有没有办法做到这一点?

For testdiceware[index].word ,the value in [] should be the index of list testdiceware,you cannot put 11111 into it.The exception ArgumentOutOfBounds is thrown when the value of an argument is outside the allowable range of values as defined by the invoked method.You can try to use对于testdiceware[index].word[]中的值应该是列表 testdiceware 的索引,不能将11111放入其中。当参数的值超出允许的值范围时,抛出异常ArgumentOutOfBounds调用方法。您可以尝试使用

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

to get word of the first testdiceware .得到第一个testdiceware的消息。

Update:更新:

you can try to use:你可以尝试使用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM