【问题标题】:Can we populate dropdown with class metadata [duplicate]我们可以用类元数据填充下拉列表吗?
【发布时间】:2018-07-20 06:10:27
【问题描述】:

这是我的视图模型类:

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>

编辑

基于 Taylor wood 的回答。我创建了一个小示例代码。在这里。

型号代码

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; }
}

控制器和动作代码

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>  

【问题讨论】:

  • 您好,我有一个问题,您为什么要这样?老实说,从课程到选项似乎有点混乱。我会怎么做?反射,stackify.com/what-is-c-reflection,您可以使用它来操作然后填充您的 DropDown。
  • 您需要编写自己的HtmlHelper 方法来执行此操作,或者根据要在DropDownListFor() 方法中使用的注释的属性和值构建IEnumerable&lt;SelectListItem&gt;
  • @StephenMuecke 你想说什么不清楚。您能否提供位代码示例以提示执行此操作。
  • 您可以使用反射来读取类的每个属性及其属性。但这真的没有意义,而且很难理解你为什么要这样做。
  • 就像我在你过去的几个问题中告诉你的那样:不要ask a new question for every hurdle you encounter while trying to do something,而是阅读How to Ask 并首先研究它。你倾倒的是“你是怎么做到的” 问题,没有显示你已经尝试过什么。创建一个 POC,先自己尝试一下。

标签: c# asp.net-mvc


【解决方案1】:

您可以使用反射来获取属性名称及其各自的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};

【讨论】:

  • 感谢您的回答。如何使用 @Html.DropDownListFor(x =&gt; x.SelectedName, x.Text) 之类的项目填充 razor 的下拉列表?
  • 此代码p.GetCustomAttribute&lt;Display&gt;() 未编译。请告诉我要添加什么作为名称空间。我添加了using System.ComponentModel.DataAnnotations; using System.Reflection; using System.ComponentModel; 告诉我我还需要做什么才能运行此代码?
  • 这样就修复了p.GetCustomAttribute&lt;DisplayAttribute&gt;()
【解决方案2】:

基于@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}) 
}

在 View 中只显示你的 selectlistitem

【讨论】:

  • 此代码 p.GetCustomAttribute() 未编译。请告诉我要添加什么作为名称空间。我使用 System.ComponentModel.DataAnnotations 添加;使用 System.Reflection;使用 System.ComponentModel;告诉我我还需要做什么才能运行此代码?
  • system.reflection 就足够了,通过这个 msdn 博客 msdn.microsoft.com/en-us/library/…
猜你喜欢
  • 2014-09-06
  • 2012-04-10
  • 1970-01-01
  • 2011-01-20
  • 2012-08-13
  • 1970-01-01
  • 2014-12-25
相关资源
最近更新 更多