ASP.NET MVC 4 avoid generation of data-val-date for datetime

后端 未结 3 474
感情败类
感情败类 2020-12-19 05:34

how can I avoid the generation of the html attribute \"data-val-date\" for the element created from a Datetime property?

The model:

public class Regi         


        
相关标签:
3条回答
  • 2020-12-19 06:08

    Add this to your application start in your global.asax file and the form should fire.

    DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;
    
    0 讨论(0)
  • 2020-12-19 06:20

    data-val-date is used by the validation system to validate the date. If you remove it, client-side validation won't work.

    If that's what you want, then just disable client-side validation.

    0 讨论(0)
  • 2020-12-19 06:23

    Ok, this took me an afternoon of work... apparently mvc4 decided that it was time to render a data-val-date="Message" on EVERY datetime property on the viewmodel. I've tried to modify this default behaviour but didn't succeed.

    This solved my problems:

    $.validator.addMethod('date',
        function (value, element) {
            return true; // since MVC4 data-val-date is put on EVERY vm date property. Default implementation does not allow for multiple cultures...
        });
    

    You can also try to write your own editor template named "DateTime.cshtml" in your shared EditorFor folder, but don't use TextBoxFor there, because that one is polluted as well.

    0 讨论(0)
提交回复
热议问题