在mvc3服务器端代码中获取显示注释值
有没有办法在服务器端代码中获取注释的值?例如,我有:
public class Dummy
{
[Display(Name = "Foo")]
public string foo { get; set; }
[Display(Name = "Bar")]
public string bar { get; set; }
}
我希望能够在服务器端获取值 "Foo" 而不将其发布回页面,但就像类的属性或类似的东西。就像 @Html.LabelFor(model => model.Foo) 但在 C# 服务器代码中。
这可能吗?
谢谢。
Is there a way to get the value of an annotation in server side code? For example, I have:
public class Dummy
{
[Display(Name = "Foo")]
public string foo { get; set; }
[Display(Name = "Bar")]
public string bar { get; set; }
}
I want to be able to get the value "Foo" on server side with out posting it back to the page, but like an attribute of the class, or something of the sort. Like a @Html.LabelFor(model => model.Foo) But in c# server code.
Is that possible?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
像这样的东西吗?
或者,如果您希望能够针对实例调用该方法并利用类型推断:
Something like this?
Or, if you want to be able to call the method against an instance and take advantage of type inference:
您将需要使用反射。这是一个示例控制台程序,可以执行您想要的操作。
这将产生以下结果:
http://www.codetunnel.com/content/images /reflectresult.jpg
You will need to use reflection. Here is a sample console program that does what you want.
This would produce the following result:
http://www.codetunnel.com/content/images/reflectresult.jpg