簡體   English   中英

ASP.NET MVC使用視圖中的值填充DropDownList

[英]ASP.NET MVC Populating a DropDownList with a value from the view

在我的控制器中,我有:

ViewData["maskList"] = new SelectList(equipmentRepository.GetMasks(), "Id", "DisplayName");

然后我將其綁定到我的視圖

<div each="var nfa in mfa.NasalFittingAssessment">
    ${Html.DropDownList("NasalMaskTypeId", ViewData["maskList"] as IEnumerable<System.Web.Mvc.SelectListItem>, new { class = "ddl" })}                
</div>

請注意,我使用的是火花視圖引擎,因此此DropDownList通過循環呈現到頁面。 這意味着下拉列表的選定值將在循環的每次迭代中更改。

我無法解決的問題是如何根據當前正在呈現到屏幕的值傳入我想要將DropDownList設置為的值。

因此,經過一番嘗試,我得出了以下結論:

您要做的就是像這樣在控制器中分配通用集合:

ViewData["maskList"] = equipmentRepository.GetMasks();

然后從這樣的視圖中動態構建選擇列表:

<div each="var nfa in mfa.NasalFittingAssessment">
    ${Html.DropDownList("NasalFittingAssessment.NasalMaskType.Id", new SelectList(ViewData["maskList"] as IList<Equipment>, "Id", "DisplayName", nfa.NasalMaskType.Id), new { class = "ddl" })}                
</div>

暫無
暫無

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

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