我有以下课程:
public partial class debiteur : Domain.EntitiesServices.IEFEntity
{
public debiteur()
{ }
[Required(ErrorMessage="The naam field is required. Please specify it to continue.")]
public string naam { get; set; }
public int id { get; set; }
}
注意道具naam
是必需的。另请注意,它继承了Domain.EntitiesServices.IEFEntity
。我有更多继承自Domain.EntitiesServices.IEFEntity
的课程。我想重用我的部分,我成功了。验证工作但验证消息没有出现!
我有一个包含此代码的主视图:
<div class="form-group ">
@Html.LabelFor(model => model.Debiteur.naam, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Debiteur.naam, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Debiteur.naam, "", new { @class = "text-danger" })
</div>
</div>
生成以下HTML:
<div class="form-group ">
<label class="control-label col-md-2" for="Debiteur_naam">naam</label>
<div class="col-md-10">
<input class="form-control text-box single-line" data-val="true" data-val-required="The naam field is required. Please specify it to continue." id="Debiteur_naam" name="Debiteur.naam" type="text" value="Deb 19" />
<span class="field-validation-valid text-danger" data-valmsg-for="Debiteur.naam" data-valmsg-replace="true"></span>
</div>
</div>
如果我点击提交按钮而没有填写naam
字段(让它保持沉默),我在<span>
- 标记之间正确收到错误消息:
<div class="form-group ">
<label class="control-label col-md-2" for="Debiteur_naam">naam</label>
<div class="col-md-10">
<input class="input-validation-error form-control text-box single-line" data-val="true" data-val-required="The naam field is required. Please specify it to continue." id="Debiteur_naam" name="Debiteur.naam" type="text" value="" />
<span class="field-validation-error text-danger" data-valmsg-for="Debiteur.naam" data-valmsg-replace="true">The naam field is required. Please specify it to continue.</span>
</div>
</div>
现在我创建了后续的部分视图(不介意if语句,它很复杂,但它有效并且不是问题的一部分!):
@model Domain.EntitiesServices.IEFEntity
@{ string pk = Model.GetPkName(); }
@foreach (var item in Model.GetType().GetProperties())
{
if (item.Name != pk && item.PropertyType.Name.ToLower().Contains("icollection") != true && item.GetGetMethod().IsVirtual != true)
{
<div class="form-group ">
@Html.Label((string)ViewBag.SelectedTable + "_" + item.Name, item.Name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.Editor(item.Name, "", (string)ViewBag.SelectedTable + "." + item.Name, new { htmlAttributes = new { @id = ViewBag.SelectedTable + "_" + item.Name, @class="form-control" } })
@Html.ValidationMessage((string)ViewBag.SelectedTable + "." + item.Name, new { @class = "text-danger" })
</div>
</div>
}
}
我从主视图中这样称呼:
@{Html.RenderAction("ViewName", "ControllerName", new { EFEntity = Model.Debiteur, selectedTable = "debiteur" });}
哪个完全相同的HTML:
<div class="form-group ">
<label class="control-label col-md-2" for="debiteur_naam">naam</label>
<div class="col-md-10">
<input class="form-control text-box single-line" data-val="true" data-val-required="The naam field is required. Please specify it to continue." id="debiteur_naam" name="debiteur.naam" type="text" value="Deb 19" />
<span class="field-validation-valid text-danger" data-valmsg-for="debiteur.naam" data-valmsg-replace="true"></span>
</div>
</div>
但是现在当我提交带有naam
道具的表格时,验证成功运行(即没有保存任何内容并且表格被回发)但我看不到错误消息。