Lamloumi Afif
Lamloumi Afif

Reputation: 9081

Adding the attribute Required to an input Text

i have an asp.net mvc4 application in which i have :

 <li>
                @Html.Label("Login")
                @Html.TextBox("Login")

            </li>

i'd like to add the attribute Required to the TextBox login like this

<input type="Text name="login" Required /> 

how can i do it?

Upvotes: 0

Views: 59

Answers (2)

Saket Kumar
Saket Kumar

Reputation: 4835

An alternate way can be to add data annotation in model:

[Required(ErrorMessage = "Field  cannot be blank")]

Upvotes: 2

ssilas777
ssilas777

Reputation: 9764

Try this

@Html.TextBox(
    "Login", 
    null, 
    new { 
        required = "required" 
    }
)

Upvotes: 1

Related Questions