示例#1
0
        /// <summary>
        /// Creates the sorter, loading its data from the route parameters or from the session, or creating
        /// a new sorter
        /// </summary>
        /// <param name="controllerContext">The controller context</param>
        /// <param name="bindingContext">The binding context</param>
        /// <returns>The <see cref="Sorter{T}"/></returns>
        private Sorter <T> MaterialiseSorter(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            ValueProviderResult valueProviderResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);

            //If the sorter cannot be gotten from the value provider)
            if (valueProviderResult == null)
            {
                //Try getting the sorter out of the session, if a session key has been set
                if (SessionKey != null && controllerContext.HttpContext.Session[SessionKey] != null)
                {
                    return((Sorter <T>)controllerContext.HttpContext.Session[SessionKey]);
                }

                //Else return a new sorter
                return(CreateNewSorter());
            }

            string encodedString = valueProviderResult.AttemptedValue;

            try
            {
                if (TranslationDictionary == null)
                {
                    return(ValidateDecodedSorter(Sorter <T> .Decode(Prefix, encodedString)));
                }

                return(ValidateDecodedSorter(Sorter <T> .Decode(Prefix, TranslationDictionary, encodedString)));
            }
            catch (SorterException)
            {
                return(CreateNewSorter());
            }
        }