从存储库填充下拉列表
我有一个来自控制器上这段代码的 MVC3 下拉列表。
private SelectList progCodesList = new SelectList(new[] { "Description", "Requirements", "Development", "Testing", "Documentation" });
如何填充存储库中的字段以动态构建下拉列表? 谢谢。
I have an MVC3 drop down list that come from this code on the controller.
private SelectList progCodesList = new SelectList(new[] { "Description", "Requirements", "Development", "Testing", "Documentation" });
How can I fill the fields from a repository, to build the drop down dynamically?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设数据库表中有
progCodes
,其中 progCode 具有文本,progCodeId 具有唯一 id,那么您可以将该表读入SelectListItem
列表,如下所示:然后您可以在强类型模型中或使用 ViewBag 将此
List
传递到您的视图。Assuming you have the
progCodes
in a database table, with progCode having the text, and progCodeId with a unique id, then you can read the table into a list ofSelectListItem
as follows:You can then pass this
List<SelectListItem>
to your view either in a strongly-typed model, or using the ViewBag.您需要使用以下内容将 progCodesList 传递到控制器方法中的 ViewBag:
然后在您的视图中,您需要像这样填充下拉列表:
You need to pass the progCodesList to the ViewBag in your controller method using something like:
Then in your view, you need to fill the drop down like this: