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

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

问题描述

希望能找到当MVC5自定义属性或preferable的RegularEx pressionAttribute模型中的装饰属性的方式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" />

或者类似的东西。所以对于RegularEx pressionAttribute 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的不显眼的选项此输出。所以我想在某种程度上来指定,获取下推到视图模型中的一些属性。不知道如果数据注解提供商可以做这个工作。不知道,如果一个助手可以扩展到得到这样的结果。

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.

帮助是AP preciated。

Help is appreciated.

推荐答案

如果使用标准的佣工超负荷添加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; }

和修改助手来解析所有的HTML属性定义。

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

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

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