We are currently using FluentValidation in our MVC project. We needed to be able to create a dynamic view where users could add or remove items. This is accomplished using partialviews.
<div id="LocationsContainer">
@foreach (var location in Model.Locations)
{
Html.RenderPartial("_Location", location);
}
@Html.ValidationMessageFor(m => m.Locations)
<br />
</div>
And within the partial view I just have a few fields.
...
<div class="float-box">
<div class="label">
@Html.LabelFor(m => m.PropertyAddress)
</div>
@Html.TextBoxFor(m => m.PropertyAddress)
<br />
@Html.ValidationMessageFor(m => m.PropertyAddress)
</div>
<div class="float-box">
<div class="label">
@Html.LabelFor(m => m.ApartmentNo)
</div>
@Html.TextBoxFor(m => m.ApartmentNo)
<br />
@Html.ValidationMessageFor(m => m.ApartmentNo)
</div>
...
In my validator i set the validator for Model.Locations (which creates the partial views) by calling SetCollectionValidator
RuleFor(vm => vm.Locations).SetCollectionValidator(new ServiceAddressViewModelValidator());
In my controller where I call ModelState.IsValid it seems to be working/validating. I can see that errors are caught inside my partial view according to my validation rules. I just can't get the error messages to display. In my validation result i can see error messages but there not getting applied to the UI. Am i doing something wrong here? I even tried using dataannotations with fluentvalidation for things other than the partial views and I got weird behavior. Doing this approach it appeared when fluentvalidation displayed error messages then the data annotation error messages didn't display and when the data annotation error messages displayed it only worked for the first partial view. If i had more than one it wasn't getting applied to the other views. Any ideas how i can get this to work? (With fluentvalidation or data annotations)?