我们可以使用类元数据填充下拉列表

时间:2018-02-09 12:56:26

标签: c# asp.net-mvc

这是我的视图模型类:

public class CustomerEditViewModel
{
    [Display(Name = "Customer Number")] 
    public string CustomerID { get; set; }

    [Display(Name = "Customer Name")] 
    public string CustomerName { get; set; }

    [Display(Name = "Customer Country")] 
    public string Country { get; set; }
}

我需要使用此类的属性填充下拉列表,因此属性名称将为值,Display.Name将成为下拉项的文本。

<select> HTML看起来像这样:

<select>
  <option value="">--Select--</option>
  <option value="CustomerID">Customer Number</option>
  <option value="CustomerName">Customer Name</option>
  <option value="Country">Customer Country</option>
</select>

修改

基于泰勒木的答案。我创建了一个小样本代码。在这里。

型号代码

public class MySkills
{
    public IEnumerable<SelectListItem> Skills
    {
        get;
        set;
    }  
}  

public class CustomerEditViewModel
{
    [Display(Name = "Customer Number")]
    public string CustomerID { get; set; }

    [Display(Name = "Customer Name")]
    public string CustomerName { get; set; }

    [Display(Name = "Customer Country")]
    public string Country { get; set; }
}

控制器&amp;行动代码

public class HomeController : Controller
    {

         public ActionResult Index()
        {
            var items = from p in typeof(CustomerEditViewModel).GetProperties()
                        let name = p.GetCustomAttribute<DisplayAttribute>().Name
                        select new SelectListItem() { Text = name, Value = p.Name };

            var ClassData = new MySkills();

            var selectList = new List<SelectListItem>();
            foreach (var item in items)
            {
                selectList.Add(new SelectListItem
                {
                    Value = item.Value.ToString(),
                    Text = item.Text
                });
            }
            ClassData.Skills = selectList;

            return View(ClassData);
        }
}

视图

@model WebTestDropDown.Controllers.MySkills
@{
    ViewBag.Title = "Home Page";
}
<br /><br /><br /><br />
<tr>
    <td> Populating With Model Data </td>
    <td> @Html.DropDownList("ClassData", Model.Skills) </td>
</tr>  

2 个答案:

答案 0 :(得分:2)

您可以使用反射来获取属性名称及其各自的Display.Name属性值:

var modelType = typeof(CustomerEditViewModel);
var properties = modelType.GetProperties();
foreach (var property in properties) {
  var displayAttr = property.GetCustomAttribute<DisplayAttribute>();
  Console.WriteLine(string.Format("{0}->{1}", property.Name, displayAttr.Name));
}

打印以下内容:

CustomerID->Customer Number
CustomerName->Customer Name
Country->Customer Country

然后根据它构建您的选择列表项。例如,使用LINQ:

var items = from p in typeof(CustomerEditViewModel).GetProperties()
            let name = p.GetCustomAttribute<DisplayAttribute>().Name
            select new SelectListItem() {Text = p.Name, Value = name};

答案 1 :(得分:0)

基于@Taylor Wood回答

var modelType = typeof(CustomerEditViewModel);
var properties = modelType.GetProperties();
List<SelectListItme>yourList = new List<SelectListItme>();
foreach (var property in properties) {
  var displayAttr = property.GetCustomAttribute<Display>();
   yourList.Add(new 
   SelectListItem{Text=Property.Name,Value=displayAttr.Name,Selected=false}) 
}

在视图中只显示selectlistitem