I am using batch editing in a Grid. Validation does not work in all cases. For example, if I add a record and the first two fields are textboxes and both are required, the grid will notify me if I don't have a value in the first textbox and try to save. However, if I fill out the first textbox but leave the second textbox blank and click on another record I don't get any message stating a value is required for the 2nd textbox. Worse, when I press the save button the grid attempts to save the record and an error occurrs because I'm trying to insert a record with a null value in a required field.
I'm using ORM to create the entities model, and then I'm adding data annotations (see below):
private string _HookNum;
[Required(ErrorMessage = "Required")]
[StringLength(15, ErrorMessage = "Max 15 Characters")]
public virtual string HookNum { get { return this._HookNum; } set { this._HookNum = value; } }
private string _HookName;
[Required(ErrorMessage = "Required")]
[StringLength(30, ErrorMessage = "Max 30 Characters")]
public virtual string HookName { get { return this._HookName; } set { this._HookName = value; } }
Remember, the first textbox (HookNum) is validated correctly, but the second (HookName) is not.
I have client events specified for trapping errors on the grid (see below):
//Happens on client (ajax)
.ClientEvents(e => {
e.OnDataBinding("Grid_onDataBinding");
e.OnError("Grid_onError");
})
And the javascript function is specified (see below):
function Grid_onError(args) {
if (args.textStatus == "modelstateerror" && args.modelState) {
var message = "Errors:\n";
$.each(args.modelState, function (key, value) {
if ('errors' in value) {
$.each(value.errors, function () {
message += this + "\n";
});
}
});
alert(message);
}
}
If I click on the second textbox, then press save, then approprate validation occurrs. I have seen other posts that suggest this is a known limitation of batch editing mode, but surely there must be a reasonable method of traping server errors and presenting error messages in the grid.
What am I missing?