Solve the problem of using Html. DropDownListFor in ASP. NET MVC4 to display the default enumerated values,
Html. DropDownListFor has provided support for Enum since ASP. net mvc 5. Before that, you need to use the help or extension method to display the enumerated values for Html. DropDownListFor.
This article solves the problem of displaying default items in Html. DropDownListFor under ASP. net mvc 4.
In the preceding example, Select is implemented through Html. DropDownListFor. The Select option value is read from enumeration. As you can see, an enumerated value becomes the default option, and I want "=" to be selected as the default option of Select.
Let's reproduce the problem before finding a solution.
There is such an enumeration.
public enum Status
{
[EnumDisplayName ("enabled")]
Enabled = 0,
[EnumDisplayName ("disabled")]
Disabled = 1
}
The Status attribute of a View Model is used to store an enumerated value.
public class MyChoice
{
public short Status { get; set; }
}
Here we need a help method to read the EnumDisplayName and enumeration value of enumeration.
The first is custom features.
public class EnumDisplayNameAttribute : Attribute
{
private string _displayName;
public EnumDisplayNameAttribute(string displayName)
{
_displayName = displayName;
}
public string DisplayName
{
get
{
return _displayName;
}
}
}
Then, the help method uses reflection to read the metadata and finally obtain a set of SelectListItem.
/// <summary>
/// Convert enumeration to SelectListItem
/// </summary>
public class EnumExt
{
/// <summary>
/// Obtain an Attribute value of the custom Attribute of the enumerated Member
/// </summary>
/// <Param name = "e"> enumeration member </param>
/// <returns></returns>
public static string GetEnumDescription(object e)
{
// Obtain the Type object of the enumerated Member
Type t = e.GetType();
// Obtain all fields of the Type object
FieldInfo[] ms = t.GetFields();
// Traverse all fields
foreach (FieldInfo f in ms)
{
if (f.Name != e.ToString())
{
continue;
}
if (f.IsDefined(typeof(EnumDisplayNameAttribute), true))
{
return (f.GetCustomAttributes(typeof(EnumDisplayNameAttribute), true)[0] as EnumDisplayNameAttribute).DisplayName;
}
}
return e.ToString();
}
public static List<SelectListItem> GetSelectList(Type enumType)
{
List<SelectListItem> selectList = new List<SelectListItem>();
// SelectList. Add (new SelectListItem {Text = "-- select --", Value = "", Selected = true });
foreach (object e in Enum.GetValues(enumType))
{
selectList.Add(new SelectListItem { Text = GetEnumDescription(e), Value = ((int)e).ToString() });
}
return selectList;
}
}
In the controller, the set instance of SelectListItem is transmitted to the foreground through ViewData.
public ActionResult Index()
{
ViewData["s"] = EnumExt.GetSelectList(typeof(Status));
return View(new MyChoice());
}
On the view page:
@ Html. DropDownListFor (m => m. Status, (IEnumerable <SelectListItem>) ViewData ["s"], "= select = ")
Now, we have the problem mentioned at the beginning: the default Select option is an enumeration value, rather than "= ".
Because the default value of enumeration is 0, when the Select statement is displayed, the enumerated value 0 is always displayed.
Since the default value of enumeration cannot be changed, set "=" to "display the custom features of enumeration items whose enumeration value is 0 ".
public enum Status
{
[EnumDisplayName ("= select =")]
None = 0,
[EnumDisplayName ("enabled")]
Enabled = 1,
[EnumDisplayName ("disabled")]
Disabled = 2
}
Modify the part of the view page. Html. DropDownListFor does not require text.
@Html.DropDownListFor(m => m.Status,(IEnumerable<SelectListItem>)ViewData["s"])
Finally, the desired effect is obtained.