1:直接使用HTML代码写

<select name="year">
<option value="2011">2010</option>
<option value="2012">2011</option>
<option value="2013">2012</option>
<option value="2014">2013</option>
</select>

效果:

2:将枚举中的值写入到 DropdownList

假设有个枚举:

 namespace MvcDropdownList.Controllers
{
public class EnumController : Controller
{
// GET: /Enum/
private const string ZHOUYI = "语文";
private const string ZHOUER = "数学";
private const string ZHOUSAN = "物理";
private const string ZHOUSI = "化学";
private const string ZHOUWU = "英语";
private const string ZHOULIU = "C语言";
private const string ZHOUQI = "ASP.NE"; public enum Days
{
Monday =,
Tuesday = ,
Wednesday = ,
Thursday = ,
Friday = ,
Saturday = ,
Sunday =
} public static IDictionary<int, string> BindData()
{
IDictionary<int, string> dict = new Dictionary<int, string>(); dict.Add((int)Days.Monday, ZHOUYI);
dict.Add((int)Days.Tuesday, ZHOUER);
dict.Add((int)Days.Wednesday, ZHOUSAN);
dict.Add((int)Days.Thursday, ZHOUSI);
dict.Add((int)Days.Friday, ZHOUWU);
dict.Add((int)Days.Saturday, ZHOULIU);
dict.Add((int)Days.Sunday, ZHOUQI); return dict;
}
public ActionResult Index2()
{
return View();
}
}
}

拓展类:

 namespace MvcDropdownList.Models
{
public static class TestExtention
{
public static MvcHtmlString DropDownList(this HtmlHelper helper, string name, IDictionary<int,string> dict, string key, string value)
{
SelectList selectListItems = new SelectList(dict, key, value);
return helper.DropDownList(name, selectListItems);
}
}
}

前台:

 <body>
<div>
@Html.DropDownList("saaa", MvcDropdownList.Controllers.EnumController.BindData(),"Key","Value")
</div>
</body>

注意前台页面需要添加前面两个类的引用:

 @using MvcDropdownList.Models
@using MvcDropdownList.Controllers;

效果:


最新文章

  1. 在Eclipse中使用Junit进行单元测试练习 实现最大子数组和算法
  2. 12个非常实用的JavaScript小技巧
  3. &amp;和&amp;&amp;的区别
  4. JS的多线程
  5. 关于ListView中notifyDataSetChanged()刷新数据不更新原因
  6. Sharepoint 2010 创建栏 计算栏
  7. MZhong&#39;s Resume
  8. IOSFramework打包。
  9. VisualStudio自定义代码段_方法一
  10. HeadFirst设计模式
  11. servlet简介
  12. opencv 图像仿射变换 计算仿射变换后对应特征点的新坐标 图像旋转、缩放、平移
  13. ReactJS 的背景及原理
  14. SecureCRT中文显示乱码的解决方法
  15. poll和select
  16. ##1.Centos7环境准备-- openstack pike
  17. 在混合开发框架模式中,简化客户端对Web API的频繁调用
  18. GDI和内核对象区别
  19. jdk1.8api帮助文档,转载
  20. Bean named &#39;*&#39; must be of type [*], but was actually of type []

热门文章

  1. Linux C程序异常退出怎么办——core文件帮你忙
  2. linux下卸载Oracle
  3. 串口通讯超时的设置与含义(COMMTIMEOUTS)
  4. USACO 2012 Feb Cow Coupons
  5. QPS、TPS、PV等网站业务关键字释义
  6. Iphone安装Pinterest
  7. UTF-8编码中BOM的检测与删除[linux下命令]
  8. (3)三剑客之sed
  9. LPD Office插件使用指南
  10. lca最短公共祖先模板(hdu2586)