将css类添加到EditorFor或使用jquery验证电子邮件

时间:2013-11-09 18:22:26

标签: jquery asp.net-mvc-4 jquery-validate unobtrusive-validation

我的模特是:

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

当我使用EditorFor(m=>m.Email)时,UI(JQuery验证)会自动验证电子邮件地址。但我需要将form-control类插入UI控件。所以我将EditorFor更改为TextBoxFor,如:

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

但现在电子邮件验证无效。

如何将课程添加到EditorFor或添加TextBoxFor的电子邮件验证(哪种或两者都是最好的方式?)。我有其他6个属性,都很好。除此之外的一切。

2 个答案:

答案 0 :(得分:2)

使用@ Html.TextBoxFor

示例:

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

使用jQuery的脏方式:

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

答案 1 :(得分:0)

这会做你想要的......

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