Exemple #1
0
        /// <summary>
        /// Build one of the view models either for Details, Form, List.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="result"></param>
        /// <param name="modelname"></param>
        /// <param name="action"></param>
        /// <returns></returns>
        public static EntityBaseViewModel BuildViewModel <T>(EntityActionResult result, string modelname, string action, EntitySettingsHelper settings)
        {
            action = action.ToLower();
            EntityBaseViewModel viewModel = null;

            if (action == "details")
            {
                viewModel = BuildViewModelForDetails(result, modelname, action, settings);
            }

            else if (action == "edit" || action == "create")
            {
                viewModel = BuildViewModelForForm(result, modelname, action, settings);
            }

            else if (action == "deleted")
            {
                viewModel = BuildViewModelForDeleted(result, modelname, settings);
            }

            else // List/Index pages.
            {
                viewModel = BuildViewModelForIndex <T>(result, modelname, action, settings);
            }

            return(viewModel);
        }
Exemple #2
0
        /// <summary>
        /// Build the form view model.
        /// </summary>
        /// <param name="result"></param>
        /// <param name="modelname"></param>
        /// <returns></returns>
        public static EntityDeletionViewModel BuildViewModelForDeleted(EntityActionResult result, string modelname, EntitySettingsHelper settings)
        {
            var viewmodel = new EntityDeletionViewModel(modelname, "Item has been deleted.");

            PopulateViewModel(viewmodel, modelname, "ModelDetails", settings);
            return(viewmodel);
        }
Exemple #3
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));
        }
Exemple #4
0
        /// <summary>
        /// Build the form view model.
        /// </summary>
        /// <param name="result"></param>
        /// <param name="modelname"></param>
        /// <returns></returns>
        public static EntityListViewModel BuildViewModelForIndex <T>(EntityActionResult result, string modelname, string action, EntitySettingsHelper settings)
        {
            PagedList <T> items     = result.ItemAs <PagedList <T> >();
            var           viewmodel = new EntityListViewModel <T>(items, null, true);

            PopulateViewModel(viewmodel, modelname, "ModelList", settings);
            return(viewmodel);
        }
Exemple #5
0
        /// <summary>
        /// Build the details view model.
        /// </summary>
        /// <param name="result"></param>
        /// <param name="modelname"></param>
        /// <returns></returns>
        public static EntityDetailsViewModel BuildViewModelForDetails(EntityActionResult result, string modelname, string action, EntitySettingsHelper settings)
        {
            var viewmodel = new EntityDetailsViewModel();

            PopulateViewModel(viewmodel, modelname, "ModelDetails", settings);
            viewmodel.Entity = result.ItemAs <IEntity>();
            return(viewmodel);
        }
Exemple #6
0
        /// <summary>
        /// Build the form view model.
        /// </summary>
        /// <param name="result"></param>
        /// <param name="modelname"></param>
        /// <returns></returns>
        public static EntityFormViewModel BuildViewModelForForm(EntityActionResult result, string modelname, string action, EntitySettingsHelper settings)
        {
            var viewmodel = new EntityFormViewModel();

            PopulateViewModel(viewmodel, modelname, "ModelForm", settings);
            viewmodel.Entity         = result.ItemAs <IEntity>();
            viewmodel.FormActionName = action;
            return(viewmodel);
        }
Exemple #7
0
 /// <summary>
 /// Handles processing of the html pages title.
 /// </summary>
 /// <param name="result"></param>
 protected virtual void HandlePageTitle(EntityActionResult result)
 {
     // Set page title.
     if (result.Success && result.Item != null && _titleFetcher != null)
     {
         T entity = result.ItemAs <T>();
         SetPageTitle(_titleFetcher(entity));
     }
 }
Exemple #8
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));
        }
        /// <summary>
        /// Find recent items in Json based format.
        /// </summary>
        /// <param name="page"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public EntityActionResult FindByAsJson(int page, int pagesize, IList <PropertyInfo> columnProps,
                                               Func <IEntityService <T>, int, int, PagedList <T> > pagedListFetcher = null, Func <int, int, EntityActionResult> actionResultFetcher = null)
        {
            PagedList <T> result     = null;
            string        message    = string.Empty;
            string        resultText = string.Empty;
            bool          success    = false;
            int           totalPages = 0;

            try
            {
                EntityActionResult entityresult = null;
                if (actionResultFetcher != null)
                {
                    entityresult = actionResultFetcher(page, pagesize);
                }
                else if (pagedListFetcher != null)
                {
                    var svc     = EntityRegistration.GetService <T>();
                    var results = pagedListFetcher(svc, page, pagesize);
                    entityresult = new EntityActionResult(results != null, string.Empty, null, results);
                }
                else
                {
                    entityresult = FindByRecent(page, pagesize);
                }
                result     = entityresult.ItemAs <PagedList <T> >();
                totalPages = result.TotalPages;

                try
                {
                    resultText = ComLib.Web.Helpers.JsonHelper.ConvertToJsonString <T>(result, columnProps);
                    success    = true;
                }
                catch (Exception ex)
                {
                    message = "Unable to convert items of : " + typeof(T).Name + " to JSON format.";
                    ComLib.Logging.Logger.Error(message, ex);
                }
            }
            catch (Exception ex)
            {
                message = "Unable to get items for : " + typeof(T).Name;
                ComLib.Logging.Logger.Error(message, ex);
                success = false;
            }
            string jsontext = EntityJsonHelper.BuildManageJsonResult <T>(success, message, result, resultText, null);

            return(new EntityActionResult(success, message, null, jsontext));
        }
Exemple #10
0
        protected BoolMessageItem <string> IsAccessable(EntityActionResult result)
        {
            if (!result.Success && !result.IsAuthorized)
            {
                return(new BoolMessageItem <string>("Pages/NotAuthorized", false, string.Empty));
            }

            if (!result.Success && !result.IsAvailable)
            {
                return(new BoolMessageItem <string>("Pages/NotFound", false, string.Empty));
            }

            return(new BoolMessageItem <string>(string.Empty, true, string.Empty));
        }
Exemple #11
0
        /// <summary>
        /// Populate the model state with errors.
        /// </summary>
        /// <param name="result"></param>
        public static void PopulateErrors(EntityActionResult result, ModelStateDictionary modelState, IList <string> stringErrors = null, IErrors errors = null)
        {
            if (result.Success || result.Errors == null || !result.Errors.HasAny)
            {
                return;
            }

            if (modelState["Id"] != null)
            {
                modelState.Remove("Id");
            }
            if (result.Errors.HasAny)
            {
                modelState.AddErrors(result.Errors);
            }

            if (errors != null && errors.Count > 0)
            {
                modelState.AddErrors(errors);
            }
        }
Exemple #12
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);
 }
Exemple #13
0
        /// <summary>
        /// Handles the management view.
        /// </summary>
        /// <returns></returns>
        public virtual ActionResult ManageUsingQuery(int page, int pagesize, IQuery <T> query = null)
        {
            EntityActionResult result = _helper.Manage(page, pagesize, query);

            return(BuildActionResult(result, _viewSettings.PageLocationForManage));
        }