How can I set MAXLENGTH to input fields automatically?

I am using ASP.NET MVC RC2. I have a set of classes that were automatically generated by "Linq-to-SQL Classes", where each class models a table in my database. I have some default "right" views that use the neat-o helper extension Html.TextBoxForto render HTML input fields for columns in a table.

However, I noticed that the attribute was not specified in the generated HTML MAXLENGTH. A quick google shows that you can add explicit attributes with TextBoxFor, etc., but that means I need to hard code the values ​​(and I need to remember that I need to do this for each input).

This seems pretty bad, considering that everything is automatically generated directly from the database, so the size of the columns is known. (Although, to be fair, the web side should work with any model objects, and not just Linq-to-SQL objects).

Is there a good way to get this to do the right thing?

+3
source share
3 answers

I would not worry about the maximum length attribute for your html elements, this is a false sense of security.

, StringLength , . , , .

ASP.NET MVC 2, .

, Linq to SQL... .

HTHS,

+3

MVC/jQuery data-val-maxlength-max maxlength JavaScript/TypeScript:

$container.find('[data-val-maxlength-max]')
    .each((index, item) => {
        var $this = $(item);
        var maxLength = $this.attr('data-val-maxlength-max');
        $this.attr('maxlength', maxLength);
    });
0

Charles, I had the same problem and worked out a solution this morning. Here's a blog post with source code.

-1
source

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


All Articles