I tried to change the model and revalidate it without success.
After binding, ModelState
has all values and errors.
If I change the model, and remove the errors and run TryValidateModel()
it doesn't revalidate the model.
If I run ModelState.Clear();
then ModelState
will be with no values.
If I run TryValidateModel()
again, the ModelState
still having no values. and the validation is not completed.
The entity is a composite class with some nested attributes. The user is able to change the nested and delete or add some of then. I would like to revalidade it because, one of the nested attrib could be not valid. However user is able to delete is. If it is deleted, them i would remove it from the model and later revalidate again. However Im not able to do it.
I just want to revalidate everything. How can I do it?
Here is my Model:
class NestedType{
public int propA {get;set;}
public int propb {get;set;}
}
class MyType {
[Key] public int Id {get;set;}
public virtual IEnumerable<NestedType> nesteds {get;set;}
}
Here is my code:
[HttpPost]
[ValidateAntiForgeryToken]
public virtual ActionResult Create([Bind()] MyType entity)
{
// Model "entity" is already binded
TryValidateModel(entity);
// Model state here has All Keys, Values for the entity
entity.nesteds.Remove(...)
// Here I Removed some nesteds objects removed by the user.
TryValidateModel(entity);
// Nothing changes (it should be revalidated, however this not happens)
// the removed nesteds still in the ModelState
ModelState.Clear();
// ModelState with no values
TryValidateModel(entity); //THIS DON'T WORK
// ModelState still with 0 values.
// the removed entries is gone, however all entries too.
// If there was another error in the model, this information was lost
if (ModelState.IsValid) //true, but it does means nothing, because validation has no values.
{...