我认为这是不好的做法,但有人可以确认吗?它是在get上填充的模型对象中的一组变量。
public List<SelectListItem> YearsAtAddressList
{
get
{
List<SelectListItem> YearsAtAddressList = new List<SelectListItem>();
YearsAtAddressList.Add(new SelectListItem { Text = "Please select...", Value = "Please select..." });
YearsAtAddressList.Add(new SelectListItem { Text = "Less than 1 year", Value = "0" });
YearsAtAddressList.Add(new SelectListItem { Text = "1", Value = "1" });
YearsAtAddressList.Add(new SelectListItem { Text = "2", Value = "2" });
YearsAtAddressList.Add(new SelectListItem { Text = "3", Value = "3" });
YearsAtAddressList.Add(new SelectListItem { Text = "4", Value = "4" });
YearsAtAddressList.Add(new SelectListItem { Text = "5", Value = "5" });
YearsAtAddressList.Add(new SelectListItem { Text = "6", Value = "6" });
YearsAtAddressList.Add(new SelectListItem { Text = "7", Value = "7" });
YearsAtAddressList.Add(new SelectListItem { Text = "8", Value = "8" });
YearsAtAddressList.Add(new SelectListItem { Text = "9", Value = "9" });
YearsAtAddressList.Add(new SelectListItem { Text = "10", Value = "10" });
YearsAtAddressList.Add(new SelectListItem { Text = "11", Value = "11" });
YearsAtAddressList.Add(new SelectListItem { Text = "12", Value = "12" });
YearsAtAddressList.Add(new SelectListItem { Text = "13", Value = "13" });
YearsAtAddressList.Add(new SelectListItem { Text = "14", Value = "14" });
YearsAtAddressList.Add(new SelectListItem { Text = "15", Value = "15" });
YearsAtAddressList.Add(new SelectListItem { Text = "16", Value = "16" });
return YearsAtAddressList;
}
private set { }
}
//DOB Days
public List<SelectListItem> Days
{
get
{
List<SelectListItem> Days = new List<SelectListItem>();
Days.Add(new SelectListItem { Text = "DD", Value = "DD" });
Days.Add(new SelectListItem { Text = "1", Value = "1" });
Days.Add(new SelectListItem { Text = "2", Value = "2" });
Days.Add(new SelectListItem { Text = "3", Value = "3" });
Days.Add(new SelectListItem { Text = "4", Value = "4" });
Days.Add(new SelectListItem { Text = "5", Value = "5" });
Days.Add(new SelectListItem { Text = "6", Value = "6" });
Days.Add(new SelectListItem { Text = "7", Value = "7" });
Days.Add(new SelectListItem { Text = "8", Value = "8" });
Days.Add(new SelectListItem { Text = "9", Value = "9" });
Days.Add(new SelectListItem { Text = "10", Value = "10" });
Days.Add(new SelectListItem { Text = "11", Value = "11" });
Days.Add(new SelectListItem { Text = "12", Value = "12" });
Days.Add(new SelectListItem { Text = "13", Value = "13" });
Days.Add(new SelectListItem { Text = "14", Value = "14" });
Days.Add(new SelectListItem { Text = "15", Value = "15" });
Days.Add(new SelectListItem { Text = "16", Value = "16" });
Days.Add(new SelectListItem { Text = "17", Value = "17" });
Days.Add(new SelectListItem { Text = "18", Value = "18" });
Days.Add(new SelectListItem { Text = "19", Value = "19" });
Days.Add(new SelectListItem { Text = "20", Value = "20" });
Days.Add(new SelectListItem { Text = "21", Value = "21" });
Days.Add(new SelectListItem { Text = "22", Value = "22" });
Days.Add(new SelectListItem { Text = "23", Value = "23" });
Days.Add(new SelectListItem { Text = "24", Value = "24" });
Days.Add(new SelectListItem { Text = "25", Value = "25" });
Days.Add(new SelectListItem { Text = "26", Value = "26" });
Days.Add(new SelectListItem { Text = "27", Value = "27" });
Days.Add(new SelectListItem { Text = "28", Value = "28" });
Days.Add(new SelectListItem { Text = "29", Value = "29" });
Days.Add(new SelectListItem { Text = "30", Value = "30" });
Days.Add(new SelectListItem { Text = "31", Value = "31" });
return Days;
}
private set { }
}
答案 0 :(得分:3)
这绝对是一种不好的做法,因为每当你调用你的财产的getter时,整个集合就会再次创建。 更好的做法是在get部分的范围之外创建此列表,然后每次调用getter时返回已填充的列表:
List<SelectListItem> _yearsAtAddressList = PopulateList();
public List<SelectListItem> YearsAtAddressList
{
get
{
return _yearsAtAddressList;
}
private set { }
}
然后PopulateList()方法看起来像这样:
public List<SelectListItem> PopulateList()
{
var yearsAtAddressList = new List<SelectListItem>();
yearsAtAddressList.Add(new SelectListItem { Text = "Please select...", Value = "Please select..." });
yearsAtAddressList.Add(new SelectListItem { Text = Less than 1 year", Value = "0" });
for (int i = 0; i < 15; i++)
{
yearsAtAddressList.Add(new SelectListItem { Text = i, Value = i });
}
return yearsAtAddressList;
}