Reputation: 487
I am working on NopCommerce customization. I am using one selected list. And I want to make one value called Active is as default. My code is as below -
foreach (StatusEnum item in Enum.GetValues(typeof(StatusEnum)))
{
newCycleModel.AvailableStatuses.Add(new SelectListItem()
{
Text = item.ToString(),
Value = ((int)item).ToString(),
});
}
test.AvailableStatuses.Insert(0, new SelectListItem() { Text = 'All', Value = "0" });
In StatusEnum i have two status as 'Active' and 'Close'
I want to put Active as default. How can I achieve this?
Upvotes: 1
Views: 474
Reputation: 102753
You could just set the Selected
property based on the current item:
newCycleModel.AvailableStatuses.Add(new SelectListItem()
{
Text = item.ToString(),
Value = ((int)item).ToString(),
Selected = (item == StatusEnum.Active)
}
This should result in the select item being selected if you use in the View:
@Html.DropDownList("myddlist", Model.AvailableStatuses)
Upvotes: 1
Reputation: 6401
Set the "Selected" property on the desired item to true.
http://msdn.microsoft.com/en-us/library/system.web.mvc.selectlistitem.selected(v=vs.108).aspx
Upvotes: 0