ASP.NET MVC 使用视图中的值填充 DropDownList

发布于 2024-08-31 11:51:58 字数 558 浏览 2 评论 0原文

在我的控制器中,我有这个:

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>

Note 将其绑定到我的视图,因为我正在使用 Spark 视图引擎,因此这个 DropDownList 通过循环呈现到页面。这意味着下拉列表的选定值将在循环的每次迭代中发生变化。

我无法解决的是如何根据当前呈现到屏幕的值传递我想要将 DropDownList 设置为的值。

In my controller I have this:

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

and then I bind it to my view using

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

Note that I'm using the spark view engine so this DropDownList is getting rendered to the page via a loop. This means that the selected value of the dropdown list will change on each iteration of the loop.

What I can't work out is how to pass in the value I want to set the DropDownList to based on the value that is currently being rendered to the screen.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

江南烟雨〆相思醉 2024-09-07 11:51:58

因此,经过一番尝试后,我解决了这个问题:

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

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>

So after a bit of playing around I worked it out:

What you want to do is just assign the generic collection in the controller like this:

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

and then construct the selectlist on the fly from within the view like this:

<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文