Hello everyone,
I'm working on an ASP.NET MVC (.NET Framework) project and encountering server-side validation issues when the form is dynamically created using JavaScript.
The actual context:
The initial form doesn't have enough inputs.
The user clicks the "Add Row" button to add new inputs using JavaScript.
These inputs are bound to a List<Model> in the Controller.
My problem:
Validation using Data Annotations ([Required], [StringLength]) doesn't always work correctly.
ModelState.IsValid returns true even though some dynamic fields are empty.
When resubmitting the form, an error message appears indicating incorrect mapping to the dynamically created inputs.
I've tried:
Setting the name correctly according to the format ListName[index].Property
Using EditorFor combined with JavaScript to clone the template
Checking ModelState.Keys in the Controller
But the results are still unstable, especially when the user adds/removes rows multiple times.
I'd like to ask the community:
What is the standard way to handle server-side validation for dynamic forms in ASP.NET MVC?
Are there any recommended patterns to avoid index errors when binding lists?
How do people usually handle displaying errors for dynamic input?
I look forward to receiving practical experience sharing from everyone.
Thank you for reading and feedback.