我有以下部分视图
@model Token.Creator.Site.Models.Customer
@{
var cusModel = new Token.Creator.Site.Models.CustomerModel();
ViewContext.FormContext = new FormContext();
HtmlHelper.ClientValidationEnabled = true;
HtmlHelper.UnobtrusiveJavaScriptEnabled = true;
}
<div class="modal fade" id="customereditmodal" tabindex="-1" role="dialog" aria-hidden="true">
@using (Ajax.BeginForm("EditCustomer", "Customer", new AjaxOptions(), new {
@class = "modal-dialog form-horizontal",
id = "upload_s0pin"
})) {
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
@if (string.IsNullOrEmpty(Model.Companyname)) {
<h4 class="modal-title">@Model.Gender.Type @Model.Title @Model.Firstname @Model.Lastname</h4>
} else {
<h4 class="modal-title">@Model.Companyname</h4>
}
</div>
<div class="modal-body">
@Html.ValidationSummary()
<div class="form-group">
@Html.LabelFor(cus => cus.Companyname, "Firmenname", new { @class = "col-lg-3 control-label" })
<div class="col-lg-9">
@Html.TextBoxFor(cus => cus.Companyname, new { @class = "form-control" })
</div>
@Html.ValidationMessageFor(cus => cus.Companyname);
</div>
<h4>Ansprechpartner</h4>
<div class="form-group">
@Html.LabelFor(cus => cus.Gender, "Anrede*", new { @class = "col-lg-3 control-label" })
<div class="col-lg-9">
@Html.DropDownListFor(cus => cus.Gender, new SelectList(cusModel.Gender.ToList(), cusModel.Gender), new { @class = "form-control" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(cus => cus.Title, "Titel", new { @class = "col-lg-3 control-label" })
<div class="col-lg-9">
@Html.TextBoxFor(cus => cus.Title, new { @class = "form-control" })
</div>
@Html.ValidationMessageFor(cus => cus.Title);
</div>
<div class="form-group">
@Html.LabelFor(cus => cus.Firstname, "Vorname*", new { @class = "col-lg-3 control-label" })
<div class="col-lg-9">
@Html.TextBoxFor(cus => cus.Firstname, new { @class = "form-control" })
</div>
@Html.ValidationMessageFor(cus => cus.Firstname);
</div>
<div class="form-group">
@Html.LabelFor(cus => cus.Lastname, "Nachname*", new { @class = "col-lg-3 control-label" })
<div class="col-lg-9">
@Html.TextBoxFor(cus => cus.Lastname, new { @class = "form-control" })
</div>
@Html.ValidationMessageFor(cus => cus.Lastname);
</div>
<div class="form-group">
@Html.LabelFor(cus => cus.Emailaddress, "Emailadresse*", new { @class = "col-lg-3 control-label" })
<div class="col-lg-9">
@Html.TextBoxFor(cus => cus.Emailaddress, new { @class = "form-control" })
</div>
@Html.ValidationMessageFor(cus => cus.Emailaddress);
</div>
<h4>Adresse</h4>
<div class="form-group">
@Html.LabelFor(cus => cus.Street, "Straße und Hausnummer*", new { @class = "col-lg-3 control-label" })
<div class="col-lg-9">
@Html.TextBoxFor(cus => cus.Street, new { @class = "form-control" })
</div>
@Html.ValidationMessageFor(cus => cus.Street);
</div>
<div class="form-group">
@Html.LabelFor(cus => cus.ZIPCode, "Postleitzahl*", new { @class = "col-lg-3 control-label" })
<div class="col-lg-9">
@Html.TextBoxFor(cus => cus.ZIPCode, new { @class = "form-control" })
</div>
@Html.ValidationMessageFor(cus => cus.ZIPCode);
</div>
<div class="form-group">
@Html.LabelFor(cus => cus.Town, "Stadt*", new { @class = "col-lg-3 control-label" })
<div class="col-lg-9">
@Html.TextBoxFor(cus => cus.Town, new { @class = "form-control" })
</div>
@Html.ValidationMessageFor(cus => cus.Town);
</div>
<div class="form-group">
@Html.LabelFor(cus => cus.Country, "Land*", new { @class = "col-lg-3 control-label" })
<div class="col-lg-9">
@Html.TextBoxFor(cus => cus.Country, new { @class = "form-control" })
</div>
@Html.ValidationMessageFor(cus => cus.Country);
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Schließen</button>
<input type="submit" class="btn btn-primary" value="Speichern" />
</div>
</div>
</div>
}
</div>
模型类由 EF 自动生成。我将其扩展为应用验证属性。
[MetadataType(typeof(CustomerMetadata))]
public partial class Customer {
private class CustomerMetadata {
[Required(ErrorMessage = "Geben Sie einen Vornamen ein")]
public object Firstname;
[Required(ErrorMessage = "Geben Sie einen Nachnamen ein")]
public object Lastname;
[Required(ErrorMessage = "Geben Sie eine Straße ein")]
public object Street;
[Required(ErrorMessage = "Geben Sie eine Postleitzahl ein")]
public object ZIPCode;
[Required(ErrorMessage = "Geben Sie eine Stadt ein")]
public object Town;
[Required(ErrorMessage = "Geben Sie ein Land ein")]
public object Country;
[Required]
[DataType(DataType.EmailAddress, ErrorMessage = "Bitte geben Sie eine gültige Emailadresse ein")]
public object Emailaddress;
}
}
但是验证不起作用。我究竟做错了什么?
编辑:部分视图由 AJAX 加载