0

Recently I've been working on a decently complex UI in which I've had to adjust the default collection binding to use a 1 based collection instead of 0 based which ends up with inputs like the following when using Html.InputFor():

 <input name="Model.Collection[1].Options.SomeOption" id="Model_Collection_1__Options_SomeOption" />
 <input name="Model.Collection[2].Options.SomeOption" id="Model_Collection_2__Options_SomeOption" />

Initially this does not work with way MVC's model binder works by default, but I came across this post by Phil Haack detailing how to use whatever you want for the model binder to bind to for the collection.http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx/ This made what I'm doing in the UI trivial, but now when I try to validate using fluent validation the name that fluent validation returns with the (correct) error message is index - 1. For instance, if in the above example Option1 is required and I leave it blank, fluent validation returns the name:

 Model.Collection[0].Options.SomeOption

I am validating my collection using the following fluent validation code:

RuleFor(x => x.Collection).SetCollectionValidator(new OptionValidator());

What I'm wondering is if I'm screwed and have to rewrite the UI or if there's some way I can force the name fluentvalidation gives me to match the name in the UI. I'm using fluentvalidation version 6.4

PS I've been looking at this frustrated for a while, so if the question isn't super clear or more information is needed forgive me, I can edit the question for more context if needed.

NOTACAT Studio
  • 217
  • 1
  • 5
  • 18

0 Answers0