【发布时间】:2017-02-07 18:53:31
【问题描述】:
我的目标是创建一个自定义属性,例如 System.ComponentModel.DataAnnotations.Display,它允许我传递参数。
例如:在 System.ComponentModel.DataAnnotations.Display 我可以将值传递给参数名称
[Display(Name = "PropertyName")]
public int Property { get; set; }
我想做同样的事情,但在控制器和操作中,如下所示
[CustomDisplay(Name = "Controller name")]
public class HomeController : Controller
然后用其值填充 ViewBag 或 ViewData 项。
我该怎么做?
【问题讨论】:
-
你必须反思控制器类型,使用
GetCustomAttributes使用ViewContext.Controller参考this -
CustomAttributes 不允许在 ViewBag 或 ViewData 中存储数据
-
参考this answer,然后将结果赋值给
ViewBag
标签: c# asp.net-mvc custom-attributes