ASP.NET MVC 使用视图中的值填充 DropDownList
在我的控制器中,我有这个:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因此,经过一番尝试后,我解决了这个问题:
您要做的就是像这样在控制器中分配通用集合:
然后从视图中动态构建选择列表,如下所示:
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:
and then construct the selectlist on the fly from within the view like this: