Randolf Rincón Fadul Randolf Rincón Fadul - 6 months ago 19
Javascript Question

Add css class to EditorFor or validate email with jquery

My Model is:

[Required]
[DataType(DataType.EmailAddress)]
[Display(Name="Email Address")]
public string Email { get; set; }


When I use
EditorFor(m=>m.Email)
then the UI (JQuery Validation) validates the email address automaticaly. But I need to insert the
form-control
class to the UI control. So I changed
EditorFor
to
TextBoxFor
like:

@Html.TextBoxFor(m => m.Email , new { @class = "form-control" })


But now the email validation is not working.

How can I either add the class to the
EditorFor
or add the email validation for the
TextBoxFor
(which or both is the best way?). I have other 6 properties and all are working great. All but this.

Answer

use @Html.TextBoxFor

Example:

@Html.TextBoxFor(x => x.Created, new { @class = "form-control" })

Or dirty way using jQuery:

$(document).ready(function () {
    $('#Email').addClass('form-control');
});