如何添加项目静态列表中的MVC Html.DropDownList() [英] How to add static list of items in MVC Html.DropDownList()

查看:283
本文介绍了如何添加项目静态列表中的MVC Html.DropDownList()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想指定的的SelectList() Html.DropDownList()在ASP.NET MVC中,什么是最好的做法是什么?

I would like to assign a static list of items in a SelectList() to a Html.DropDownList() in ASP.NET MVC, what is the best practice?

我正要试图找到一种方法,使用新的SelectList(新{键=值} ... 只有一个,没有工作,二,我会在这里打破了法律,应我的静态列表在的ViewData 反正声明,并通过为的IList / IEnumerable的

I was about to try to find a way to use new SelectList(new {key = "value"}... but one, that didn't work, and two, would I be breaking a law here, should my static list be declared in ViewData anyway and passed as IList/IENumerable?

推荐答案

OK,我决定把我自己的建议,这应该在控制器中定义的:

OK I decided to take my own advice, and this should be defined in the controller:

仅供参考,我刚回到:

PageData data = new PageData()
           {
               Formats = new[]
                             {
                                 new { ID = "string", Name = "Text" },
                                 new { ID = "int", Name = "Numeric" },
                                 new { ID = "decimal", Name = "Decimal" },
                                 new { ID = "datetime", Name = "Date/Time" },
                                 new { ID = "timespan", Name = "Stopwatch" }
                             },
               .............

           };
return View(data);

...(忽略上下文)和在View ASPX侧

... (ignore context) and in the View ASPX side:

<%= Html.DropDownList("type.field", new SelectList(ViewData.Model.Formats, "ID", "Name"...

如果任何人有这样做的更优化的方式,我会很乐意接受他们的答案。

If anyone has a more optimal way of doing this I'll be happy to accept their answer.

这篇关于如何添加项目静态列表中的MVC Html.DropDownList()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆