@Html.DropDownListFor(model => model.Status, new List<SelectListItem>
{ new SelectListItem{Text="Active", Value="True"},
new SelectListItem{Text="Deactive", Value="False"}})
In view I am using this drop dowenlist coding. I run my application default deactive value is display in dropdown list box. I want display default Active
This question is related to
c#
asp.net-mvc
razor
dropdownbox
I hope this is helpful to you.
Please try this code,
@Html.DropDownListFor(model => model.Items, new List<SelectListItem>
{ new SelectListItem{Text="Deactive", Value="False"},
new SelectListItem{Text="Active", Value="True", Selected = true},
})
try this
@Html.DropDownListFor(model => model.UserName, new List<SelectListItem>
{ new SelectListItem{Text="Active", Value="True",Selected =true },
new SelectListItem{Text="Deactive", Value="False"}})
SelectListItem
has a Selected
property. If you are creating the SelectListItem
s dynamically, you can just set the one you want as Selected = true
and it will then be the default.
SelectListItem defaultItem = new SelectListItem()
{
Value = 1,
Text = "Default Item",
Selected = true
};
Source: Stackoverflow.com