Html.ValidationSummaryFor未显示

时间:2014-11-02 22:26:15

标签: asp.net-mvc data-annotations

未显示第一个名称和姓氏的验证摘要。在我的数据库中,我将这些字段设置为" Not Null"所以我认为这些是必需的。我遇到的另一个问题是我设置的底部附近的一些字段" Null"需要验证吗?

型号:

    [DisplayName("First Name")]
    public string FirstName { get; set; }

    [DisplayName("Middle Initial")]
    public string MiddleInitial { get; set; }

    [DisplayName("Last Name")]
    public string LastName { get; set; }

查看:

        <div class="col-md-4">
            @Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @class = "form-control boldgray", @placeholder = "First Name" } })
            @Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" })

        </div>

        <div class="col-md-1 col-md-offset-1 col-md-push-half">
            @Html.EditorFor(model => model.MiddleInitial, new { htmlAttributes = new { @class = "form-control boldgray", @placeholder = "M.I." } })
            @Html.ValidationMessageFor(model => model.MiddleInitial, "", new { @class = "text-danger" })

        </div>

        <div class="col-md-4 col-md-push-2">
            @Html.EditorFor(model => model.LastName, new { htmlAttributes = new { @class = "form-control boldgray", @placeholder = "Last Name" } })
            @Html.ValidationMessageFor(model => model.LastName, "", new { @class = "text-danger" })

        </div>
    </div>

2 个答案:

答案 0 :(得分:2)

您需要添加Required属性。

试试这个:

[Required(ErrorMessage="First Name Required")]
[DisplayName("First Name")]
public string FirstName { get; set; }

答案 1 :(得分:0)

您必须添加[必需]才能使验证正常工作。

[Required]
[DisplayName("First Name")]
public string FirstName { get; set; }

[Required]
[DisplayName("Middle Initial")]
public string MiddleInitial { get; set; }

[Required]
[DisplayName("Last Name")]
public string LastName { get; set; }