So, if I want to add a new object to my database, I can write this:
public ActionResult Something(SomeObject Object) {
if (ModelState.IsValid()) {
DataContext.SomeObjects.InsertOnSubmit(Object);
DataContext.SubmitChanges();
};
}
But, how does validation get called when I want to update an object? Does UpdateModel<T> automatically perform the validation or do I have to do something to tell it to or do I have to do something ahead of calling UpdateModel<t>?
EDIT:
For @SLaks, so do I do something like this:
UpdateModel<SomeObject>(Object);
if (ModelState.IsValid()) {
DataContext.SubmitChanges();
};
Or, if I’m butchering that, please show me the right way.
Your original question seemed to be “how does validation get called?”.
The validation is called on the object if it implements the
IDataErrorInfointerface.IDataErrorInfois implemented in the Model Binder – this happens just before the execution is passed to your Action.So, answer in short: You don’t have to tell it to do something if your using
DataAnnotations. The validation happens automatically before you get to the Action’s code. That’s whyModelState.IsValid()returns a value – because the validation has completed.