使用 C# 在 ASP.NET MVC 3 中创建级联下拉列表的最简单方法
我想使用 MVC3
(最好是 Razor
)和 C#
在级联中创建两个 DropDownList
代码>.
我想要一个下拉菜单,您可以在其中选择年份,还有另一个下拉菜单,您可以根据所选年份选择一组特定的月份。
让我们简单地说。当我在下拉列表“年”中选择当前年份(即 2011 年)时,下拉列表“月”将填充当前月份(即 3 月)之前的月份。对于其他情况(其他年份)则不予限制。此外,在选择下拉列表“年”中的任何元素之前,最好“阻止”下拉列表“月”。
我已经在 Internet 上查找了一些解决方案,使用 jQuery 甚至自制方法,但它们都引用了 MVC 的过去版本,并且一些命令在 MVC3 中已弃用。
I want to create two DropDownList
in a cascade using MVC3
(preferably Razor
) with C#
.
I would like to have one dropdown where you can choose the year and another one where you can choose a specific set of months depending on the selected year.
Let's put it simple. When I choose the current year (i.e. 2011) in the dropdown list "year", the dropdown list "month" gets populated with the months until the current month (i.e. March). For the other cases (other years) no restriction is given. Moreover it would be nice to "block" the dropdown list "month" before any element in the dropdown list "year" is selected.
I already looked in the Internet for some solutions, using jQuery
or even homemade approaches, but they all refer to past versions of MVC and some commands are deprecated in MVC3
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
与往常一样,您从模型开始:
然后是控制器:
最后是视图:
显然您会注意到在我的示例中我已经对所有值进行了硬编码。您应该通过使用诸如当前年份、当前月份之类的概念来改进此逻辑,甚至可能从存储库中获取这些值等......但出于演示的目的,这应该足以让您走上正确的轨道。
As always you start with a model:
then a controller:
and finally a view:
Obviously you will notice that in my example I have hardcoded all the values. You should improve this logic by using notions like current year, current month, probably even fetch those values from a repository, etc... but for the purpose of the demonstration this should be enough to put you on the right track.