How to create a field for viewing razors?

I want to add the attributes "data-val-required" and "data-val" to the @ html.textbox or @ Html.EditorFor element. Is this possible without overwriting the view?

+4
source share
1 answer

Normally, you should not rewrite submission to achieve this. You must decorate your view model properties with the appropriate validation attributes. For instance:

[Required] public string Foo { get; set; } 

Then the HTML helpers generate the correct markup. But if for some strange reason you cannot change this code, you can use javascript to add these attributes manually:

 $(function() { $('#id_of_the_field').attr('data-val-required', 'true'); }); 

After adding these attributes, you need to revise the form validation rules containing these input fields so that your changes take effect:

 $('form').removeData('validator'); $('form').removeData('unobtrusiveValidation'); $.validator.unobtrusive.parse('body'); 
+9
source

Source: https://habr.com/ru/post/1403642/


All Articles