public ActionResult T2() { TestModel tm = new TestModel() { Name = "Namessss", }; return View(tm); } public class TestModel { [Test("csss")] [UIHint("LayuiText")] public string Name { get; set; } }
[AttributeUsage(AttributeTargets.Property)] public sealed class TestAttribute : Attribute, IMetadataAware { public TestAttribute(string displayName) { this.DisplayName = displayName; } /// <summary> /// 获取或设置模型的显示名称。 /// </summary> public string DisplayName { get; private set; } public string TemplateHint { get; set; } public void OnMetadataCreated(ModelMetadata metadata) { metadata.DisplayName = this.DisplayName; metadata.TemplateHint = this.GetTemplateHint(); metadata.AdditionalValues.Add("Layui.Text.DisplayName", this.DisplayName); //throw new NotImplementedException(); } /// <summary> /// 获取TemplateHint值,如果TemplateHint值为空则使用特性名。 /// </summary> /// <returns>TemplateHint值。</returns> private string GetTemplateHint() { return string.IsNullOrEmpty(this.TemplateHint) ? this.GetType().Name.Replace("Attribute", string.Empty) : this.TemplateHint; } }