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?

11
  • 3
    uuu this looks ugly, why would u do that? Commented Apr 29, 2015 at 13:41
  • here is the answer: stackoverflow.com/questions/2493143/… Commented Apr 29, 2015 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. Commented Apr 29, 2015 at 13:48
  • 2
    Why not use AdditionalMetadata? Commented Apr 29, 2015 at 13:49
  • Because the Business layer, Therefore the models are not in an MVC project. We have seperated the two entirely. Commented Apr 29, 2015 at 13:51

2 Answers 2

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.

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

1 Comment

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

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.