programing

필요하지 않은 속성이 계속 data-val-required 속성을 가져옵니다.

copysource 2021. 1. 17. 11:51
반응형

필요하지 않은 속성이 계속 data-val-required 속성을 가져옵니다.


이것이 검증 된 모델입니다.

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

그리고 다음은 렌더링되는 내용입니다.

<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 속성에 필요한 특성이 설정되지 않은 경우 TagId 유효성 검사가 생성된다는 것입니다. 그 때문에 db에 새 태그를 생성하기 위해 클라이언트 측 유효성 검사를 통과 할 수도 없습니다. 내가 무엇을 놓치고 있습니까?


답을 찾았습니다. 이것을 다음에 추가하십시오 Application_Start:

DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;

뷰 모델 값 유형을 nullable로 만듭니다. 그러면 기본적으로 필수가 아닙니다.

또한 html 5에 'required = "false"'속성을 입력하면 (doctype 메타 데이터에 html 5를 설정 한 경우) "required"가 표시되고 필수로 설정됩니다. dojo-data-props = "required : false"를 사용할 수 있습니다.


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

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.


jquery validate target cheking "disabled" html attribute.

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

or use Nullable.

hope this code!


With MVC4 you can also use this:

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

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.

ReferenceURL : https://stackoverflow.com/questions/4700172/unrequired-property-keeps-getting-data-val-required-attribute

반응형