3

Does anybody know a way to turn off MVC3 automatically decorating primitive types with a data-val-* attribute.

DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;

removes the data-val-required attribute, but I can't seem to find a way to turn off primitive types eg: data-val-number

I have a lot of hidden int fields which don't required validating on a form, but because of these attributes they are getting validated, causing my app to appear frozen.

Lisa Young
  • 240
  • 1
  • 4
  • 10
  • 1
    Can you remove the DataAnnotations attributes for those hidden fields? – jrummell May 22 '12 at 13:24
  • This post should help disabling unobtrusive validation attributes for primitive types: [http://stackoverflow.com/a/7322464/1604300](http://stackoverflow.com/a/7322464/1604300) – cocco3 Sep 11 '12 at 18:35

4 Answers4

0

I imagine that the hidden int fields have the [Required] data annotations defined on them in the viewmodel? If so then I believe you just need to remove the data annotation to prevent the data-val-required attribute from being displayed.

I could be wrong, but I suspect you will then say that the field is required when that viewmodel is used in some other views?

If this is the case, then rather than turning off the data annotations (which is essentially a work around) then you need to define your view models correctly. Ideally, each view model should be specific for the view that it is defined (see pattern 3 of the following link). This will avoid the issues where you have fields that are required on some views and are not required on others.

Dangerous
  • 4,818
  • 3
  • 33
  • 48
  • The int fields within the view model are not decorated with any data annotations, the problem I have is that the validation is slowing my app down on hidden fields that don't need validating. MVC is decorating these int fields with data-val-number, causing them to be validated on submit. – Lisa Young May 23 '12 at 07:57
  • I see what you mean now. I was using string types before but indeed, if you use an int data type with no data annotation you do get the data-val-required and data-val-number attributes. However, i'm still not sure why this is a problem. If you are specifying a hidden int field then there should be a provided int value in that field in which case the validation should pass anyway (and i'm not sure that it would slow down the app somewhat as this is client side validation). – Dangerous May 23 '12 at 08:37
  • If there isn't a value for that hidden field could you just not render that field in the view? That way when you perform a post action there will be no value to post and the binder will just use a default value? There is a similar question [here](http://stackoverflow.com/questions/1353029/conditionally-validating-portions-of-an-asp-net-mvc-model-with-dataannotations) with Phil Haacks answer being the answer of interest for myself. – Dangerous May 23 '12 at 08:38
  • Its a very complex model with lots of lists of items, these hidden fields are mainly id's for the lists, these need to have values but the values are checked server side, the client side validation is checking they contain numbers which I don't need it to do, creating my own html helper solved the problem. Thanks for your help. – Lisa Young May 23 '12 at 09:40
0

You can specify data-val="false" in the HTML input which you are creating on the page, for example:

<input type="checkbox" name="foo" value="@item.foo" class="input-validation-error"
 data-val="false">
Kobi
  • 135,331
  • 41
  • 252
  • 292
Parag Vyas
  • 73
  • 5
  • Thank you, I wanted to created the html input using html helpers and the HiddenFor helper was adding the primitive type annotations. I created my own html helper to get around this, and have posted the code in a separate answer. – Lisa Young May 23 '12 at 08:03
0

I couldn't seem to find a way to turn this off, so created my own HtmlHelper as a way to get around this issue.

public static IHtmlString HiddenInputFor<TModel, TValue>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TValue>> expression, IDictionary<string, object> htmlAttributes)
    {
        ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, helper.ViewData);
        var memberExpression = (MemberExpression)expression.Body;
        string fullID = helper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(memberExpression.Member.Name);
        var builder = new TagBuilder("input");
        builder.MergeAttribute("type", "hidden");
        var value = ModelMetadata.FromLambdaExpression(expression, helper.ViewData).Model;
        builder.MergeAttribute("value", value.ToString());
        string fullName = helper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(ExpressionHelper.GetExpressionText(expression));
        builder.MergeAttribute("name", fullName);
        builder.GenerateId(fullID);
        var tag = builder.ToString(TagRenderMode.SelfClosing);
        return new HtmlString(tag);
    }
Lisa Young
  • 240
  • 1
  • 4
  • 10
0

I've noticed that if you load a partial view from an ajax request, the validations (data-val-*) inside the partial view are not automatically added. So I finally changed my code to load from ajax the heavy form data that doesn't need validations.