簡體   English   中英

如何從 Razor 中的不同模型填充下拉列表?

[英]How do I populate a dropdown from a different model in Razor?

我正在處理一個項目,我有一個主模型,該模型需要包含許多其他模型並將它們顯示在主模型的視圖上。 我似乎無法從他們那里帶來我需要在下拉列表中填充的數據。

我的一個較小的模型是 Entity,它看起來像這樣:

   public class Entity
{
    //[Required]
    public int ID { get; set; }

    [Required]
    public string Name { get; set; }

    [Required]
    public string Active { get; set; }
}

這是我試圖將選擇列表項引入的標記語言:

<div class="form-group">
                    <label asp-for="SecurityLog.EntityID" class="control-label"></label>
                    <select asp-for="SecurityLog.EntityID" class="form-control switch-disable" asp-items="EntityID">
                        <option value="">--Select Entity--</option>
                    </select>
                    <span asp-validation-for="SecurityLog.EntityID" class="text-danger"></span>
                </div>

在我的 EditModel 頁面上,我創建了一個實體 viewData 並且無法讓結果出現在標記中。 它被稱為“實體ID”:

            ViewData["EntityID"] = new SelectList(_context.Entity.Where(a => a.Active == "Y"), "ID", "Name");

我似乎無法讓 ViewData 顯示任何內容,並且在下午的大部分時間里都在努力。 提前致謝!

你很近...

ViewData EntityID 屬性可以通過ViewBag在剃刀頁面上訪問

剃刀頁

<select id="entity" asp-for="SecurityLog.EntityID" class="form-control" asp- 
        items="ViewBag.EntityID">
    <option value="">--Select Entity--</option>
</select>

OnGetAsync

ViewData["EntityID"] = new SelectList(_context.Entity.Where(a => a.Active == "Y"), "ID", "Name");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM