Unrequired property keeps getting data-val-required attribute

Posted by frennky on Stack Overflow See other posts from Stack Overflow or by frennky
Published on 2011-01-15T14:45:03Z Indexed on 2011/01/15 14:54 UTC
Read the original article Hit count: 318

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?

© Stack Overflow or respective owner

Related posts about jquery-validate

Related posts about asp.net-mvc-3