user2900970
user2900970

Reputation: 761

ModelState for EditorTemplate

I am using an editor template for a complex type (for example an employee). I do this because I want to edit several employees in one view. This works fine. Now I need to add a validation error from the controller. But when I simply use

Modelstate.AddModelError

The error is added to my "outer" Model, not the model my editor template is working with. How can I pass through the error?

Thanks for any suggestions!

Upvotes: 0

Views: 302

Answers (1)

Réda Mattar
Réda Mattar

Reputation: 4381

You can specify which property of your "outer model" (or view model) is concerned by this validation issue :

ModelState.AddModelError("Employee.FirstName", "FirstName is required");

This will target MyOuterModel.Employee.FirstName.

This works if you have one Employee object in your view model. If you work with a collection of employees, you can write string.Format("Employee[{0}].FirstName", i).

Upvotes: 1

Related Questions