不需要的属性不断获取数据

不需要的属性不断获取数据

本文介绍了不需要的属性不断获取数据-VAL要求的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是与它的模型的验证:

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; }
    //...
}

下面认为:

    <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>

和这里得到什么的renderd:

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>

问题是,TAGID验证获取生成althoug thare没有必需属性上TAGID属性设置。正因为如此,我甚至无法通过客户端验证,才能在数据库创建新的标签。
我缺少什么?

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?

推荐答案

我找到了答案。只需添加这的Application_Start

I found the answer. Just add this to Application_Start:

DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;

这篇关于不需要的属性不断获取数据-VAL要求的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 08:43