Voglio avere una proprietà di lunghezza massima impostata in un campo di testo in html.
Ho il seguente codice nel mio codice rasoio:
<td>
<label asp-for="AspNetUser.FirstName" ></label>
</td>
<td>
<input type="text" asp-for="AspNetUser.FirstName" asp-maxlength />
</td>
La mia definizione per FirstName è:
[Display(Name = "First Name")]
[MaxLength(50)]
public string FirstName { get; set; }
Ottengo il seguente risultato:
<td>
<label for="AspNetUser_FirstName">First Name</label>
</td>
<td>
<input type="text" data-val="true" data-val-maxlength="The field First Name must be a string or array type with a maximum length of '50'." data-val-maxlength-max="50" id="AspNetUser_FirstName" name="AspNetUser.FirstName" value="Wallace" />
</td>
Se metto un valore che supera i 50 caratteri, c'è un errore sul troncamento, che mi aspetterei. Tuttavia, mi aspetterei che da qualche parte sarei in grado di ottenere la proprietà della lunghezza massima e quindi usarla per assicurarmi che venga utilizzata la lunghezza massima. Come ottengo questo valore di attributo?
TIA.
Quanto segue dovrebbe funzionare e non consentire voci più lunghe di 50 caratteri:
<td>
<label asp-for="AspNetUser.FirstName" ></label>
</td>
<td>
<input type="text" asp-for="AspNetUser.FirstName" maxlength="50" />
</td>