gpt4 book ai didi

c# - ASP.NET MVC 使用 View 中的值填充 DropDownList

转载 作者:太空宇宙 更新时间:2023-11-03 14:30:28 25 4
gpt4 key购买 nike

在我的 Controller 中我有这个:

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

然后我使用

将它绑定(bind)到我的 View
<div each="var nfa in mfa.NasalFittingAssessment">
${Html.DropDownList("NasalMaskTypeId", ViewData["maskList"] as IEnumerable<System.Web.Mvc.SelectListItem>, new { class = "ddl" })}
</div>

请注意,我使用的是 spark View 引擎,因此此 DropDownList 通过循环呈现到页面。这意味着下拉列表的选定值将在循环的每次迭代中发生变化。

我无法解决的是如何根据当前呈现在屏幕上的值传入我想要设置 DropDownList 的值。

最佳答案

经过一番尝试,我得出了结论:

您要做的只是像这样在 Controller 中分配通用集合:

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

然后像这样在 View 中动态构建选择列表:

<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>

关于c# - ASP.NET MVC 使用 View 中的值填充 DropDownList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2752752/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com