protected ActionResult TryPush(Action <CommandComposite> push, CommandComposite composite, Action <IncTryPushSetting> action = null, bool?isAjax = null) { var setting = new IncTryPushSetting(); action.Do(r => r(setting)); Func <ActionResult> defaultSuccess = () => View(composite.Parts[0]); var isActualAjax = isAjax.GetValueOrDefault(HttpContext.Request.IsAjaxRequest()); if (isActualAjax) { defaultSuccess = () => IncodingResult.Success(); } var success = setting.SuccessResult ?? defaultSuccess; Func <IncWebException, ActionResult> defaultError = (ex) => View(composite.Parts[0]); if (isActualAjax) { defaultError = (ex) => IncodingResult.Error(ModelState); } var error = setting.ErrorResult ?? defaultError; if (!ModelState.IsValid) { return(error(IncWebException.For(string.Empty, string.Empty))); } try { push(composite); return(success()); } catch (IncWebException exception) { foreach (var pairError in exception.Errors) { foreach (var errorMessage in pairError.Value) { ModelState.AddModelError(pairError.Key, errorMessage); } } return(error(exception)); } }
protected ActionResult TryPush(CommandComposite composite, Action <IncTryPushSetting> action = null) { var setting = new IncTryPushSetting(); action.Do(r => r(setting)); Func <ActionResult> defaultSuccess = () => View(composite.Parts[0].Message); if (HttpContext.Request.IsAjaxRequest()) { defaultSuccess = () => IncodingResult.Success(); } var success = setting.SuccessResult ?? defaultSuccess; Func <IncWebException, ActionResult> defaultError = (ex) => View(composite.Parts[0].Message); if (HttpContext.Request.IsAjaxRequest()) { defaultError = (ex) => IncodingResult.Error(ModelState); } var error = setting.ErrorResult ?? defaultError; if (!ModelState.IsValid) { return(error(IncWebException.Empty)); } try { this.dispatcher.Push(composite); return(success()); } catch (IncWebException exception) { ModelState.AddModelError(exception.Property, exception.Message); return(error(exception)); } }