簡體   English   中英

下拉列表和選擇列表

[英]Drop down list with select list

這是我從模型中選擇的列表。

public IEnumerable<SelectListItem> ServiceCycleList
{
    get
    {
        var Item = new List<SelectListItem>
        {
            new SelectListItem { Text = "Yearly", Value = "12" },
            new SelectListItem { Text = "Quarterly", Value = "3" },
            new SelectListItem { Text = "Monthly", Value = "1" }
        };
        return Item;
    }
}

我想為此選擇列表創建一個下拉列表。 我該怎么辦?

如果模型視圖可以使用它,則可以這樣創建DropDownList

@Html.DropDownListFor(model => model.YourModelProperty, new SelectList(Model.ServiceCycleList, "Value", "Text"), "Please Select...")

根據上面的評論,您的操作方法將返回沒有模型的視圖,因此,如果模型為null,則會收到錯誤。

public ActionResult Index(..)
{
   .
   .

   return View(); // missing model reference
}

暫無
暫無

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

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