I have the following class:
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; }
}
Note the prop naam
is required. Also note it inherits Domain.EntitiesServices.IEFEntity
. I have more classes that inherit from Domain.EntitiesServices.IEFEntity
. I want to reuse my partial, which I succeeded in. The validation works but the validation messages don't show up!
I had a main view which contains this code:
<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>
Which produces the follwoing 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>
If I hit the submit button without filling in the naam
field(leaving it empthy) I correctly get an error message between my <span>
-tags:
<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>
Now I created the followoing partial view (don't mind the if statement, its complicated but it works and is not part of the question!):
@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>
}
}
Which I call from my main view like this:
@{Html.RenderAction("ViewName", "ControllerName", new { EFEntity = Model.Debiteur, selectedTable = "debiteur" });}
Which produces exactly the same 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>
But now when I submit the form with an empthy naam
prop the validation succesfully works(i.e. nothing is saved and form is posted back) but I see no error message.