Getting the value of the MaxLength attribute of a column in EF Core

c# ef-core-2.0 ef-core-2.1 entity-framework-core

Question

I want to have a max length property set in a text field in html.

I have the following code in my razor code:

<td>
        <label asp-for="AspNetUser.FirstName" ></label>
    </td>
    <td>
        <input type="text" asp-for="AspNetUser.FirstName" asp-maxlength />
    </td>

My definition for the FirstName is:

[Display(Name = "First Name")]
[MaxLength(50)]
public string FirstName { get; set; }

I get the following output:

<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 &#x27;50&#x27;." data-val-maxlength-max="50" id="AspNetUser_FirstName" name="AspNetUser.FirstName" value="Wallace" />
</td>

If I put a value in that exceeds 50 characters, there is an error about truncation, which I would expect. However, I would expect that somewhere I would be able to get the max length property and then use it to make sure that the maximum length is used. How do I get this attribute value?

TIA.

1
0
8/21/2018 11:27:23 AM

Popular Answer

The following should work and not allow entries longer than 50 characters:

<td>
    <label asp-for="AspNetUser.FirstName" ></label>
</td>
<td>
    <input type="text" asp-for="AspNetUser.FirstName" maxlength="50" />
</td>
0
8/21/2018 10:19:50 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow