Unrequired property keeps getting data-val-required attribute

asp.net MvcJquery Validate

asp.net Mvc Problem Overview


This is the model with it's validation:

[MetadataType(typeof(TagValidation))]
public partial class Tag
{
}

public class TagValidation
{
        [Editable(false)]
        [HiddenInput(DisplayValue = false)]
        public int TagId { get; set; }

        [Required]
        [StringLength(20)]
        [DataType(DataType.Text)]
        public string Name { get; set; }
	//...
}

Here is the view:

    <h2>Create</h2>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Tag</legend>

        <div>@Html.EditorForModel()</div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

And here is what get's renderd:

<form action="/Tag/Create" method="post">
    <fieldset>
        <legend>Tag</legend>
        <div><input data-val="true" data-val-number="The field TagId must be a number." data-val-required="The TagId field is required." id="TagId" name="TagId" type="hidden" value="" />
            
        <div class="editor-label"><label for="Name">Name</label></div>
        <div class="editor-field"><input class="text-box single-line" data-val="true" data-val-length="The field Name must be a string with a maximum length of 20." data-val-length-max="20" data-val-required="The Name field is required." id="Name" name="Name" type="text" value="" /> <span class="field-validation-valid" data-valmsg-for="Name" data-valmsg-replace="true"></span></div>            
    ...
    </fieldset>
</form>

The problem is that TagId validation gets generated althoug thare is no Required attribute set on TagId property. Because of that I can't even pass the client-side validation in order to create new Tag in db. What am I missing?

asp.net Mvc Solutions


Solution 1 - asp.net Mvc

I found the answer. Just add this to Application_Start:

DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;

Solution 2 - asp.net Mvc

Make the view-model value-types nullable. Then they won't be Required by default.

Note also if you put the attribute 'required="false"' in html 5 (if you set html 5 in your doctype meta data), it will see "required" and make it required. You can use dojo-data-props="required:false".

Solution 3 - asp.net Mvc

frennky's solution only removed data-val-required but in my case I still had data-val-number and data-val

I had to add the two lines below to Application_Start to get rid of everything.

ModelValidatorProviders.Providers.Clear();
ModelValidatorProviders.Providers.Add(new DataAnnotationsModelValidatorProvider());

Solution 4 - asp.net Mvc

The problem is that the value of the hidden field is empty. This shouldn't happen if you use integer type. I suppose that the TagId property is defined as a nullable type in the Tag class. So either assign it a value before rendering the view or use an integer type:

[MetadataType(typeof(TagValidation))]
public partial class Tag
{
    public int TagId { get; set; }
    public string Name { get; set; }
}

so that the generated hidden field looks like this:

<input 
    data-val="true" 
    data-val-number="The field TagId must be a number." 
    data-val-required="The TagId field is required." 
    id="TagId" 
    name="TagId" 
    type="hidden" 
    value="0" 
/>

Also normally client side validation shouldn't be triggered for this hidden field.

Solution 5 - asp.net Mvc

jquery validate target cheking "disabled" html attribute.

$(function () { 
  $("#TagId").attr("disabled", "disabled") 
});

or use Nullable.

hope this code!

Solution 6 - asp.net Mvc

With MVC4 you can also use this:

@{ Html.EnableClientValidation(false); }
@Html.EditorForModel()
@{ Html.EnableClientValidation(true); }

Solution 7 - asp.net Mvc

Make your Model or View-Model property value-types "nullabel". This will solve your problem.One important thing that remove "required" attribute from your tag otherwise it will take i "required"

Example:-

public class ViewModle
{
    public int? foo{get;set;}
}

Here in example foo is integer nullable type, this will no longer required in mvc.

Hope this will help you.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionfrennkyView Question on Stackoverflow
Solution 1 - asp.net MvcfrennkyView Answer on Stackoverflow
Solution 2 - asp.net MvcCurtis YallopView Answer on Stackoverflow
Solution 3 - asp.net MvcnthpixelView Answer on Stackoverflow
Solution 4 - asp.net MvcDarin DimitrovView Answer on Stackoverflow
Solution 5 - asp.net MvctakeparaView Answer on Stackoverflow
Solution 6 - asp.net Mvct2tView Answer on Stackoverflow
Solution 7 - asp.net MvcJani DevangView Answer on Stackoverflow