ASP.NET MVC 2: Manually create a list and attach to model
I have a model:
[Required]
public List<SelectList> Meals { get; set;
}
And I want to create a list in my Controller so I can attach it to "Meals" in my Model.
For some reason I'm having issues doing this:
_model.MaxCoupo开发者_开发百科ns = new List<SelectListItem>();
SelectListItem _mList = new SelectListItem([]{
new SelectListItem { Text = "---", Value = "" },
new SelectListItem { Text = "50", Value = "50" },
new SelectListItem { Text = "60", Value = "60" },
new SelectListItem { Text = "70", Value = "70" },
new SelectListItem { Text = "80", Value = "80" },
new SelectListItem { Text = "90", Value = "90" },
new SelectListItem { Text = "100", Value = "100" },
new SelectListItem { Text = "110", Value = "110" },
new SelectListItem { Text = "120", Value = "120" },
new SelectListItem { Text = "130", Value = "130" },
new SelectListItem { Text = "140", Value = "140" },
new SelectListItem { Text = "150", Value = "130" } },
"Text", "Value" );
_model.MaxCoupons.Add(_mList);
I'm a bit confused right now...
public SelectList Meals { get; set; }
public SelectList MySelectList()
{
List<SelectListItem> _returnList = new List<SelectListItem>();
SelectListItem _mList = new SelectListItem();
_mList = new SelectListItem(){ Text = "---", Value = "" };
_returnList.Add(_mList);
//keep repeating
}
then inside a method:
Meals = new MySelectList();
精彩评论