CustomAttribute 反映 html 属性 MVC5 [英] CustomAttribute reflects html attribute MVC5

查看:38
本文介绍了CustomAttribute 反映 html 属性 MVC5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

希望在MVC5中找到一种方法,当在MVC5中自定义属性或更好的RegularExpressionAttribute装饰模型中的属性时,html控件将其包含为控件的另一个属性.例如

Hoping to find a way when in MVC5 a Custom attribute or preferable the RegularExpressionAttribute decorates a property in the model the html control will contain it as another attribute of the control. E.g.

class CoolModel {
   [CustomHtmlAttribute("hello")]
   public string CoolValue {get;set;} 
}

输出...

<input type="text" customhtml="hello" />

或者类似的东西.所以对于 RegularExpressionAttribute 来说,pattern 属性会很棒.

Or something like that. So for the RegularExpressionAttribute the pattern attribute will be awesome.

class CoolModel {
   [RegularExpressionAttribute("/d")]
   public string CoolValue {get;set;} 
}

输出...

<input type="text" pattern="/d" />

我需要这个输出而不启用 Javascript unobtrusive 选项.所以我想以一种方式在模型中指定一些向下推到视图的属性.不确定数据注释提供程序是否可以完成这项工作.不确定是否可以扩展 Helper 以获得此结果.

I need this output without enabling the Javascript unobtrusive option. So I'm thinking in a way to specify some attribute in the model that gets push down to the view. Not sure if the Data annotations provider could do this job. Not sure if a Helper could be extended to get this result.

感谢帮助.

推荐答案

如果使用带重载的标准助手来添加 html 属性是不可接受的,那么您可以创建一个属性实现 IMetadataAware 来添加metadata.AdditionalValues 的属性,然后可以在自定义 html 帮助程序中使用.一个简单的例子可能是

If using the standard helpers with the overload to add html attributes is not acceptable, then you can create an attribute implements IMetadataAware that adds properties to metadata.AdditionalValues which can then be used in custom html helpers. A simple example might be

[AttributeUsage(AttributeTargets.Property)]
public class CustomHtmlAttribute : Attribute, IMetadataAware
{
  public static string ValueKey
  {
    get { return "Value"; }
  }
  public string Value { get; set; }
  public void OnMetadataCreated(ModelMetadata metadata)
  {
    if (Value != null)
    {
      metadata.AdditionalValues[ValueKey] = Value;
    }
  }
}

并创建一个帮助器来呈现文本框(此处仅显示一个重载)

and to create a helper to render a textbox (only one overload shown here)

public static MvcHtmlString CustomHtmlTextBoxFor<TModel, TValue>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TValue>> expression)
{
  ModelMetadata metaData = ModelMetadata.FromLambdaExpression(expression, helper.ViewData);
  object attributes = null;
  if (metaData.AdditionalValues.ContainsKey(ValueKey))
  {
    attributes = new { customhtml = (string)metaData.AdditionalValues[ValueKey] };
  }
  return InputExtensions.TextBoxFor(helper, expression, attributes);
}

并将其用作

[CustomHtml(Value = "hello")]
public string CoolValue { get; set; } 

在视图中

@Html.CustomHtmlTextBoxFor(m => m.CoolValue)

为了使它更灵活一点,您可以向属性添加更多属性,以便您可以将其应用为

to make this a bit more flexible, you could add more properties to the attribute so you could apply it as

[CustomHtml(Value = "hello", Pattern="/d")]
public string CoolValue { get; set; }

并修改 helper 以呈现您定义的所有 html 属性.

and modify the helper to render all the html attributes you define.

这篇关于CustomAttribute 反映 html 属性 MVC5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆