Beispiel #1
0
        /// <summary>
        /// Converts to a redirection result.
        /// </summary>
        /// <param name="result"></param>
        /// <param name="failAction"></param>
        /// <param name="redirectUrl"></param>
        /// <param name="redirectUrlRouteValues"></param>
        /// <returns></returns>
        protected virtual ActionResult BuildRedirectResult(EntityActionResult result, string successAction = null,
                                                           string successUrl = null, object routeValues = null, string viewLocationForErrors = null, string viewActionForErrors = null)
        {
            // Not authorized or Not Available ?
            var accessable = IsAccessable(result);

            if (!accessable.Success)
            {
                return(View(accessable.Item));
            }

            if (result.Success && !string.IsNullOrEmpty(successUrl))
            {
                return(Redirect(successUrl));
            }

            if (result.Success && !string.IsNullOrEmpty(successAction))
            {
                return(RedirectToAction(successAction, routeValues));
            }

            EntityViewHelper.PopulateErrors(result, ModelState);

            var viewmodel = EntityViewHelper.BuildViewModel <T>(result, this._modelname, viewActionForErrors, _settings);

            return(View(viewLocationForErrors, viewmodel));
        }
Beispiel #2
0
        /// <summary>
        /// Intialize
        /// </summary>
        public EntityController()
        {
            _settings = _entitySettingsHandler();
            var modelSettings = _settings.ModelFor <T>();

            _viewSettings = modelSettings == null ? new ModelSettings().View : modelSettings.View;
            _helper       = new EntityHelper <T>(_settings, typeof(T).Name);
            _modelname    = this.GetType().Name.Replace("Controller", "");
            EntityViewHelper.CheckAndDefaultSettings(modelSettings);
            Init();
        }
Beispiel #3
0
        /// <summary>
        /// Converts the entity result to an actionresult.
        /// </summary>
        /// <param name="result">EntityActionResult</param>
        /// <param name="successPage">Page url if result is succcessful.</param>
        /// <param name="successAction"></param>
        /// <param name="failPage"></param>
        /// <param name="failAction"></param>
        /// <returns></returns>
        protected virtual ActionResult BuildActionResult(EntityActionResult result, string successPage        = "", string successAction = "", string failPage = "", string failAction = "",
                                                         Action <EntityBaseViewModel> onAfterViewModelCreated = null, bool useDefaultFormAction = false)
        {
            // Not authorized or Not Available ?
            var accessable = IsAccessable(result);

            if (!accessable.Success)
            {
                return(View(accessable.Item));
            }

            PopulateErrors(result);

            // No action provided? default to action of url /model/action.
            if (string.IsNullOrEmpty(successAction))
            {
                if (string.IsNullOrEmpty(successPage))
                {
                    successAction = "Manage";
                }
                else
                {
                    successAction = successPage.Substring(successPage.LastIndexOf("/") + 1);
                }
            }

            // Build the appropriate viewmodel based on success/failure of entity action( create/edit ) etc.
            EntityBaseViewModel viewmodel = result.Success
                                          ? EntityViewHelper.BuildViewModel <T>(result, this._modelname, successAction, _settings)
                                          : EntityViewHelper.BuildViewModel <T>(result, this._modelname, failAction, _settings);

            // Option 1 : Default the form action to what ever it is currently ( typically used for create/edit pages for post backs ).
            if (useDefaultFormAction && viewmodel is EntityFormViewModel)
            {
                var formModel      = viewmodel as EntityFormViewModel;
                var pathAndQuery   = this.Request.Url.PathAndQuery;
                var actionAndQuery = pathAndQuery.Substring(pathAndQuery.IndexOf("/", 1) + 1);
                formModel.FormActionName = actionAndQuery;
                formModel.RouteValues    = null;
            }

            // Allow caller to do some additional processing.
            if (onAfterViewModelCreated != null)
            {
                onAfterViewModelCreated(viewmodel);
            }

            string pageLocation = result.Success ? successPage : failPage;

            return(View(pageLocation, viewmodel));
        }
Beispiel #4
0
 /// <summary>
 /// Populates errors into the model state dictionary.
 /// </summary>
 /// <param name="result"></param>
 /// <param name="stringErrors"></param>
 /// <param name="errors"></param>
 protected virtual void PopulateErrors(EntityActionResult result, IList <string> stringErrors = null, IErrors errors = null)
 {
     EntityViewHelper.PopulateErrors(result, ModelState, stringErrors, errors);
 }