【问题标题】:Get MVC2 model attribute value from Controller从 Controller 获取 MVC2 模型属性值
【发布时间】:2012-04-19 03:52:16
【问题描述】:

您好,我想知道是否有一种直接的方法可以在我的模型 VIA 控制器中检索自定义属性的值。为了争论......假设我的模型中有这个:

[DisplayName("A name")]
public string test;

在我的控制器中,我想使用类似的东西来检索“A name”:

ModelName.test.Attributes("DisplayName").value

是不是很玄幻?

提前致谢。
WML

【问题讨论】:

标签: c# asp.net asp.net-mvc-2


【解决方案1】:

Here is a good article on how to retrieve values from attributes.我认为除了反射之外没有其他方法可以做到这一点。

从文章中(只需更改示例的属性类型 :)):

   public static void PrintAuthorInfo(Type t) 
   {
      Console.WriteLine("Author information for {0}", t);
      Attribute[] attrs = Attribute.GetCustomAttributes(t);
      foreach(Attribute attr in attrs) 
      {
         if (attr is Author) 
         {
            Author a = (Author)attr;
            Console.WriteLine("   {0}, version {1:f}",
a.GetName(), a.version);
         }
      }
   }

【讨论】:

  • 对于匿名的反对者,请告诉我你为什么反对我的回答?如果我不知道问题是什么,我无法修复任何感知到的错误......
  • 感谢您的灵感回答....我创建了一个基于 [link]msdn.microsoft.com/en-us/library/system.attribute(v=vs.90).aspx 文章的例程,并使用反射来询问 propertyInfo。我阅读了有关 GetCustomAttributes 的更多信息,并且基本上与您的例程相似。非常感谢。
【解决方案2】:

试试这个:

var viewData = new ViewDataDictionary<MyType>(/*myTypeInstance*/);
string testDisplayName = ModelMetadata.FromLambdaExpression(t => t.test, viewData).GetDisplayName();

【讨论】:

  • 这是我的错,但是如果属性是自定义属性而不是 DisplayName 怎么办?我试图做的是在我的模型类中针对属性(例如测试)有一个自定义属性 [GroupId(2)]。在这种情况下,我尝试关联“测试的 groupId 为 2”。我可以通过简单地创建另一个属性来实现同样的事情,但我认为将它作为一个属性来做会更简洁一些。我很抱歉对属性的概念不太熟悉。应该阅读更多。
【解决方案3】:

反射很容易做到。 内部控制器:

 public void TestAttribute()
    {
        MailJobView view = new MailJobView();
        string displayname = view.Attributes<DisplayNameAttribute>("Name") ;


    }

扩展名:

   public static class AttributeSniff
{
    public static string Attributes<T>(this object inputobject, string propertyname) where T : Attribute
    {
        //each attribute can have different internal properties
        //DisplayNameAttribute has  public virtual string DisplayName{get;}
        Type objtype = inputobject.GetType();
        PropertyInfo propertyInfo = objtype.GetProperty(propertyname);
        if (propertyInfo != null)
        {
            object[] customAttributes = propertyInfo.GetCustomAttributes(typeof(T), true);

            // take only publics and return first attribute
            if (propertyInfo.CanRead && customAttributes.Count() > 0)
            {
                //get that first one for now

                Type ourFirstAttribute = customAttributes[0].GetType();
                //Assuming your attribute will have public field with its name
                //DisplayNameAttribute will have DisplayName property
                PropertyInfo defaultAttributeProperty = ourFirstAttribute.GetProperty(ourFirstAttribute.Name.Replace("Attribute",""));
                if (defaultAttributeProperty != null)
                {
                    object obj1Value = defaultAttributeProperty.GetValue(customAttributes[0], null);
                    if (obj1Value != null)
                    {
                        return obj1Value.ToString();
                    }
                }

            }

        }

        return null;
    }

}

我测试它工作正常。它将使用该属性的第一个属性。 MailJobView 类有一个名为“Name”的属性,带有 DisplayNameAttribute。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-10
    • 2015-05-03
    • 2013-06-06
    • 1970-01-01
    • 2015-08-23
    • 2014-03-30
    相关资源
    最近更新 更多