private bool HandleErrors(ChangeSet set) { var errorOps = set.EntityOperations.Where(op => op.HasError); if (errorOps.Count() == 0) { return(true); } foreach (var operation in errorOps) { var errors = operation.Errors ?? operation.ValidationErrors; if (operation.Errors != null && operation.ValidationErrors != null) { errors = errors.Union(operation.ValidationErrors); } if (errors != null) { foreach (var error in errors) { foreach (var name in error.SourceMemberNames) { ViewData.ModelState.AddModelError(name, error.Message); } } } if (operation.HasConflict) { ViewData.ModelState.AddModelError("_FORM", string.Format("Conflict encountered. Members in conflict are '{0}'.", string.Join(",", operation.ConflictMembers.ToArray <string>()))); } } return(false); }
bool SubmitChanges() { try { var domainservice = Context as DomainService; if (domainservice != null) { var chngSet = new ChangeSet(_changeSet); domainservice.Submit(chngSet); if (HandleErrors(chngSet)) { _changeSet.Clear(); return(true); } else { return(false); } } else { var datacontext = Context as DataContext; if (datacontext != null) { datacontext.SubmitChanges(); } else { return(false); } } return(true); } catch (Exception e) { //ViewData.ModelState.AddModelError("_FORM", e); //<-- grr, this should do that: var modelstate = ViewData.ModelState["_FORM"]; if (modelstate == null) { modelstate = new ModelState(); ViewData.ModelState["_FORM"] = modelstate; } modelstate.Errors.Add(new ModelError(e, e.Message)); return(false); } }
bool SubmitChanges() { try { var domainservice = Context as DomainService; if (domainservice != null) { var chngSet = new ChangeSet(_changeSet); domainservice.Submit(chngSet); if (HandleErrors(chngSet)) { _changeSet.Clear(); return true; } else { return false; } } else { var datacontext = Context as DataContext; if (datacontext != null) { datacontext.SubmitChanges(); } else { return false; } } return true; } catch (Exception e) { //ViewData.ModelState.AddModelError("_FORM", e); //<-- grr, this should do that: var modelstate = ViewData.ModelState["_FORM"]; if (modelstate == null) { modelstate = new ModelState(); ViewData.ModelState["_FORM"] = modelstate; } modelstate.Errors.Add(new ModelError(e, e.Message)); return false; } }
private bool HandleErrors(ChangeSet set) { var errorOps = set.EntityOperations.Where(op => op.HasError); if (errorOps.Count() == 0) return true; foreach (var operation in errorOps) { var errors = operation.Errors ?? operation.ValidationErrors; if (operation.Errors != null && operation.ValidationErrors != null) errors = errors.Union(operation.ValidationErrors); if (errors != null) { foreach (var error in errors) { foreach (var name in error.SourceMemberNames) { ViewData.ModelState.AddModelError(name, error.Message); } } } if (operation.HasConflict) { ViewData.ModelState.AddModelError("_FORM", string.Format("Conflict encountered. Members in conflict are '{0}'.", string.Join(",", operation.ConflictMembers.ToArray<string>()))); } } return false; }