【问题标题】:C# Data Annotations - Set properties Model with Display nameC# 数据注释 - 使用显示名称设置属性模型
【发布时间】:2018-09-27 07:23:43
【问题描述】:

我有一个包含模型显示名称字符串的列表:

public class TVSystemViewData : BaseViewData
    {
        [Display(Name = "AccountId", Description = "")]
        public String AccountId { get; set; }

        [Display(Name = "AllocatedManagedMemory", Description = "")]
        public String AllocatedManagedMemory { get; set; }

        [Display(Name = "AllocatedPhysicalMemory", Description = "")]
        public String AllocatedPhysicalMemory { get; set; }

        [Display(Name = "AudioMute", Description = "")]
        public String AudioMute { get; set; }
}

我需要使用 foreach 循环设置属性以将值添加到我的模型:

这是从 POST 应用程序获取值的方式

var model.AccountId = shell.getParameter("AccountId")
var model.AllocatedManagedMemory = shell.getParameter("AllocatedManagedMemory");

shell.GetParameter 从 POST 中获取值。

这就是我想要的: 我有一个获取所有显示属性的方法

public List<String> GetTVSystemProperties()
{
    return typeof(TVSystemViewData)
        .GetProperties()
        .SelectMany(x => x.GetCustomAttributes(typeof(DisplayAttribute), true) //select many because can have multiple attributes
            .Select(e => ((DisplayAttribute)e))) //change type from generic attribute to DisplayAttribute
        .Where(x => x != null).Select(x => x.Name) //select not null and take only name
        .ToList();
}

我的收藏是一个字符串列表 例如:集合 {"AccountId","AllocatedManagedMemory"...}

我的模型是 TVSystemViewData

foreach (item in collection)
{
    if(item == modelProperty name){
         // i don know how
         model.property = shell.getParameter(item)
    }
}

[更新] 我正在使用这个:

        foreach (var property in UtilsHandler.getConfigAsList("sysDataSource"))
        {
            //Set Values to Model
            try
            {
                model.GetType().GetProperty(property).SetValue(model, shell.getParameter(property), null);
            }
            catch (Exception)
            {
Have issue with data types 
            }
        }

我对数据类型有疑问。 但我使用一个 foreach 循环。 仍在寻找最佳方法

【问题讨论】:

  • 我不确定我是否理解这个问题。您正在编写自定义模型绑定器吗?

标签: c# c#-4.0 data-annotations


【解决方案1】:

你需要让类继承IEnumerator,或者自己添加一个GetEnumerator方法。

var model = new TVSystemViewData();

foreach(var item in model)
{
  item.AccountId = shell.getParameter("AccountId");
  //item.AllocatedManagedMemory ...
}

更多信息请参考这篇文章:How to make the class as an IEnumerable in C#?

查看这篇文章:https://support.microsoft.com/en-us/help/322022/how-to-make-a-visual-c-class-usable-in-a-foreach-statement

//编辑。忘记这部分了:

    List<TVSystemViewData> model;

    [Display(Name = "AccountId", Description = "")]
    public String AccountId { get; set; }

    [Display(Name = "AllocatedManagedMemory", Description = "")]
    public String AllocatedManagedMemory { get; set; }

    [Display(Name = "AllocatedPhysicalMemory", Description = "")]
    public String AllocatedPhysicalMemory { get; set; }

    [Display(Name = "AudioMute", Description = "")]
    public String AudioMute { get; set; }

    public IEnumerator<TVSystemViewData> GetEnumerator()
    {
        foreach (var item in model)
        {
            yield return item;
        }
    }

编辑根据您的更新问题:我不知道这是否可行,但应该可行。

var model = new TVSystemViewData();
        PropertyInfo[] properties = typeof(TVSystemViewData).GetProperties();
        List<string> items = new List<string> { "AccountId", "AllocatedManagedMemory" }; //your collection of strings


        foreach (var item in items)
        {
            foreach (var property in properties)
            {
                if (item == property.Name)
                {
                    property.SetValue(model, shell.getParameter(item));
                }
            }
        }

【讨论】:

  • 我更详细地更新了问题。在您的回答中,我必须始终执行 model.AccountId = some; model.AudioMute=一些;我有 68 个属性要建模
  • 我已经更新了我的答案,希望对您有所帮助。至少可能会为您指明正确的方向,也是类似的主题,如果这没有帮助,请检查一下:stackoverflow.com/questions/8151888/…
  • 我使用我使用的代码进行更新。删除你的一个 foreach。
  • 您应该添加您正在获取的错误。我试过你更新的东西,效果很好。 shell.gerParameter(item) 会返回一个字符串吗?我想,但是“UtilsHandler.getConfigAsList("sysDataSource")" 是否为您提供了正确的数据类型字符串?你为什么要迭代它而不是你提到的字符串列表?
  • shell 总是返回一个字符串。你可以用try catch包围。或者您可以使用数据类型进行切换并尝试解析...这只是一个想法
猜你喜欢
  • 2019-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-25
  • 1970-01-01
  • 1970-01-01
  • 2015-12-14
相关资源
最近更新 更多