仅适用于视图模型的某些属性的数据注释

时间:2018-05-02 12:11:38

标签: c# data-annotations

我有以下视图模型:

public class ClientViewModel
{
    public string Title { get; set; }

    [Required(ErrorMessage = "Name is required")]
    public string Name { get; set; }

    [Required(ErrorMessage = "Surname is required")]
    public string Surname { get; set; }

    [Required(ErrorMessage = "Email is required")]
    [DataType(DataType.EmailAddress)]
    public string Email { get; set; }

    [Display(Name = "Contact Number")]
    public string ContactNumber { get; set; }
}

和cshtml:

    <div class="form-horizontal">
    <h4>Client information</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })

    <div class="form-group">
        @Html.LabelFor(model => model.Title, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownListFor(model => model.Title, new SelectList(ViewBag.TitleList, "Text", "Value"), "Title")
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Surname, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Surname, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Surname, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.ContactNumber, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.ContactNumber, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.ContactNumber, "", new { @class = "text-danger" })
        </div>
    </div>

我有以下问题:

  • 所需的注释仅适用于Email字段,不适用于NameSurname字段。
  • “电子邮件”字段为空时显示的错误消息不是我在视图模型中放置的错误消息。

知道我做错了吗?

电子邮件和姓氏字段的呈现的html是:

    <div class="form-group">
        <label class="control-label col-md-2" for="Surname">Surname</label>
        <div class="col-md-10">
            <input class="form-control text-box single-line" id="Surname" name="Surname" type="text" value="" />
            <span class="field-validation-valid text-danger" data-valmsg-for="Surname" data-valmsg-replace="true"></span>
        </div>
    </div>

    <div class="form-group">
        <label class="control-label col-md-2" for="Email">Email</label>
        <div class="col-md-10">
            <input class="form-control text-box single-line" data-val="true" data-val-required="The Email field is required." id="Email" name="Email" type="email" value="" />
            <span class="field-validation-valid text-danger" data-valmsg-for="Email" data-valmsg-replace="true"></span>
        </div>
    </div>

正如您在电子邮件中看到的那样,我们有data-val-required属性,但没有Surname字段。

0 个答案:

没有答案