這是我正在使用的資料庫的影像
我想顯示“單詞”,但只使用“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 例外可能是因為它參考了這個值(標記為紅色),但我想顯示與“id”值對應的“word”
我面臨的主要障礙是:
我正在生成數字“11111”,我需要一種方法來僅使用生成的數字“11111”來顯示相應的單詞,有沒有辦法實作這一點?
uj5u.com熱心網友回復:
因為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
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/409139.html
標籤:
上一篇:如何將容器div變成內容滑塊
下一篇:從兩個表的同名兩個列中獲取資料
