3

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;} 
}

outputs...

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

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

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

outputs...

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

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.

Help is appreciated.

13
  • 1
    To achieve this you can just use the standard helpers and add html attributes @Html.TextBoxFor(m => m.SomeProperty, new { customhtml = "hello" }). Doing it using custom data annotations will be much more complex Commented Oct 23, 2014 at 0:54
  • See this link Commented Oct 23, 2014 at 0:59
  • Thanks both for the answers. @StephenMuecke I'm aware of that but I need to specify the attribute in the model for maintainability purposes. Commented Oct 23, 2014 at 1:01
  • @artm I checked that link earlier and I wonder if there is another way to do it. Could you add a meta attribute that gets to be render in the view without doing that whole process? Commented Oct 23, 2014 at 1:03
  • 1
    In that case artm's link may be useful, although its a bit out of date and does rely on creating custom EditorTemplates which can be difficult to maintain across projects. The other alternative is to implement IMetadataAware to add the attributes to metadata.AdditionalValues, then create a set of custom html helpers that merge the attributes Commented Oct 23, 2014 at 1:07

1 Answer 1

5

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);
}

and use it as

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

and in the view

@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; }

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.