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