1

I'd like to be able to make a data attribute in c# for an object in my model. When this is displayed. I'd like the html to render the value in a data attribute within the html itself.

In my head, the code looks similar to this.

public class AffectsAttribute:Attribute
{
    public string[] Departments { get; set; }

}


[EmailAddress]
[Required(ErrorMessage = "Email is required")]
[Affects(Departments = new string[]{"Coaches"})]
public string Email {get; set;}

Then in the html, after being called with razor it would look something like this

@Html.EditorFor(model => model.Email)


<input class="text-box single-line" data-affects="Coaches" data-val="true" data-val-email="The Email field is not a valid e-mail address." data-val-required="Email is required" id="Email" name="Email" type="email"  value="">

I have no clue how to specify that I would like the additional data attribute to be added when the item is rendered on a page. Can anyone help?

LiamHT
  • 1,312
  • 3
  • 12
  • 28
  • 3
    uuu this looks ugly, why would u do that? – Legends Apr 29 '15 at 13:41
  • here is the answer: http://stackoverflow.com/questions/2493143/how-do-you-read-the-value-of-an-attribute-in-a-method – Legends Apr 29 '15 at 13:42
  • @Legends I'd do that so that the developer working on the business layer can point out dependencies between areas, then we can warn users when they update. – LiamHT Apr 29 '15 at 13:48
  • 2
    Why not use [AdditionalMetadata](https://msdn.microsoft.com/en-us/library/system.web.mvc.additionalmetadataattribute(v=vs.118).aspx)? – Andrei V Apr 29 '15 at 13:49
  • Because the Business layer, Therefore the models are not in an MVC project. We have seperated the two entirely. – LiamHT Apr 29 '15 at 13:51
  • @Legends also, your answer shows me how to access them within c#. Do I have to have a custom class to tell razor how to write the html? – LiamHT Apr 29 '15 at 13:52
  • sorry, can't help you with that... – Legends Apr 29 '15 at 13:54
  • damn, Thanks anyway! – LiamHT Apr 29 '15 at 13:55
  • You don't need a custom class but you do need custom logic. You need to get the data stored by the attribute in order to *save it* in your HTML. – Andrei V Apr 29 '15 at 13:57
  • my only problem is that any model data requires me to use the MVC namespace. as my model is situated in a business layer (class library) i do not have that information presently. So even using additionalMetadata as Andrei said doesnt work... Any ideas for a work around? – LiamHT Apr 29 '15 at 15:10
  • Html helpers generate attributes based on the metadata of the property. Your attribute would need to implement `IMetadataAware` and you would need to create a custom html helper ([example here](http://stackoverflow.com/questions/26519493/customattribute-reflects-html-attribute-mvc5/26519946#26519946)). In any case claiming your model is in a business layer is irrelevant. You should always be using a view model for editing data. –  Apr 30 '15 at 00:00

2 Answers2

1

It can be done through the ValidationAttribute. If you inherit this:

public class MyCustomAttribute : ValidationAttribute
{
    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
    }

    public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
    {
    }
}

The GetClientValidationRules is what you are interested in. You return an IEnumerable of attributes you want the client to have.

You can check out this article on CodeProject.

beautifulcoder
  • 10,832
  • 3
  • 19
  • 29
  • my only problem is that any model data requires me to use the MVC namespace. as my model is situated in a business layer (class library) i do not have that information presently – LiamHT Apr 29 '15 at 14:52
0

You'll have to create a new HTML helper to do that. http://www.asp.net/mvc/overview/older-versions-1/views/creating-custom-html-helpers-cs

It's an extension from the HtmlHelper, that creates that. Something like:

  public static class YourExtensions
 {
      public static string YourMethodName(this HtmlHelper helper, string[] paramName)
      {
           return  String.Format("<input data-affects='{0}'>",String.Join(" ", paramName));
      }
 }
brduca
  • 3,573
  • 2
  • 22
  • 30