1

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.

Kip ei
  • 479
  • 6
  • 20
  • It works fine, I can edit and save my data if I met my data-validation requirements! If not I want the error message to be displayed, which is the thing I can not get working. – Kip ei Jul 20 '15 at 12:44
  • Could you post the HTML output for the div in case of error? They look the same when valid, would be nice to compare the invalid outputs as well – Volkan Paksoy Jul 20 '15 at 14:12
  • When I delibriatly post an error value(empthy string) it produces exactly the same HTML, ie nothing between the ``-tags...I double checked it like 4 times... – Kip ei Jul 20 '15 at 19:04
  • I used the accepted answer on this post: [Loop through Model properties in reflection, then use Html helpers to display. How to get concrete property back?](http://stackoverflow.com/questions/19915587/loop-through-model-properties-in-reflection-then-use-html-helpers-to-display-h) But since I am working from within a partial I have to justify it to make model binding work, and it does work accept for the validation message... – Kip ei Jul 20 '15 at 19:08

0 Answers0