傳統 ASP.NET 的 ListControl(DropDownList、RadioButtonList 等)在 MVC 中需手動指定選項。結合 ModelMetadata 與編輯器模板,可依屬性特性自動呈現對應列表控件,免除手工維護選項清單。本文示範四大列表控件的自動化實現。
定義 Employee 模型,屬性以特性標註呈現形式:
public class Employee
{
[DisplayName("姓名")]
public string Name { get; set; }
[RadioButtonList("Gender")]
[DisplayName("性別")]
public string Gender { get; set; }
[DropdownList("Education")]
[DisplayName("學歷")]
public string Education { get; set; }
[ListBox("Department")]
[DisplayName("所在部門")]
public IEnumerable<string> Departments { get; set; }
[CheckBoxList("Skill")]
[DisplayName("擅長技能")]
public IEnumerable<string> Skills { get; set; }
}
View 僅需 @Html.EditorForModel() 即可自動依特性渲染對應控件。

RadioButtonListAttribute 等)繼承 Attribute,儲存列表名稱。Views/Shared/EditorTemplates/RadioButtonList.cshtml 等),讀取 ModelMetadata 的 AdditionalValues 取得列表名稱。ListDataProvider)依名稱取得選項集合,渲染 <select>、<input type=radio> 等 HTML。UIHint 或模板命名慣例(型別名稱)讓 MVC 自動匹配模板。此模式將「資料來源」與「呈現邏輯」分離,符合關注點分離原則,極大簡化企業級應用中字典/代碼表的維護。