一般在下拉框中绑定数据的话,分为几种情况。
(1)下拉框中的数据是写死的,我们直接给出死代码即可。
(2)下拉框中的数据从数据库中读取出来,从而进行显示。
(3)下拉框中直接用枚举显示。
(4)下拉框中一个选择的值改变另外一个下拉框中的值。
一,硬编码:
ViewBag.hard_value = new List<SelectListItem>() {
new SelectListItem(){Value="0",Text="xpy0928"},
new SelectListItem(){Value="1",Text="cnblogs"}
};
下拉框读取数据库【二】
public class BlogCategory
{
public int CategoryId { get; set; }
public string CategoryName { get; set; }
}
//从数据库中读取
var categoryList = new List<BlogCategory>() {