Exemple #1
0
        public void ConvertEnumToSelectList_WhenUseIntValisTrue_ReturnsListWithNumericValues()
        {
            var result = SelectListItemUtility.ConvertEnumToSelectList(typeof(RealEstateOptionForBuyEnum), null, true,
                                                                       false);

            Assert.Equal(new [] { "0", "1", "2" }, result.Select(s => s.Value));
        }
Exemple #2
0
        public void ConvertEnumToSelectList_WhenSelectedIsNull_ReturnsListWithNoItemSelected()
        {
            var result = SelectListItemUtility.ConvertEnumToSelectList(typeof(RealEstateOptionForBuyEnum), null, false,
                                                                       false);

            Assert.DoesNotContain(result, s => s.Selected);
        }
Exemple #3
0
        public void ConvertEnumToSelectList_WhenSelectedIsSet_ReturnsListWithItemSelected(string selectedItem)
        {
            var result = SelectListItemUtility.ConvertEnumToSelectList(typeof(RealEstateOptionForBuyEnum), selectedItem, false,
                                                                       false);

            Assert.Equal(selectedItem, result.Single(s => s.Selected).Value);
        }
Exemple #4
0
        public void ConvertEnumToSelectList_WhenIncludeBlankIsTrue_ReturnsListWithFirstItemBlank()
        {
            var result = SelectListItemUtility.ConvertEnumToSelectList(typeof(RealEstateOptionForBuyEnum), null, false,
                                                                       true);

            var expectedResult = new[] { "", "Rent/Lease", "Buy", "Either" };

            Assert.Equal(expectedResult, result.Select(s => s.Text));
        }
Exemple #5
0
        public void ConvertEnumToSelectList_WhenPassedAnEnumWithDescription_ReturnsDescriptionInTextProperty()
        {
            var result = SelectListItemUtility.ConvertEnumToSelectList(typeof(RealEstateOptionForBuyEnum), null, false,
                                                                       false);

            var expectedResult = new[] { "Rent/Lease", "Buy", "Either" };

            Assert.Equal(expectedResult, result.Select(s => s.Text));
        }
Exemple #6
0
        public static IHtmlContent DropDownListFor <TModel, TProperty>(this IHtmlHelper <TModel> htmlHelper,
                                                                       Expression <Func <TModel, TProperty> > expression, Type enumType, bool useIntValue, bool includeBlank, object htmlAttributes) where TModel : class
        {
            var selected = string.Empty;

            try
            {
                var value = htmlHelper.ViewData.Model == null
                    ? default(TProperty)
                    : expression.Compile()(htmlHelper.ViewData.Model);

                selected = value == null ? string.Empty : value.ToString();
            }
            catch (Exception)
            {
                //do nothing
            }

            var list = SelectListItemUtility.ConvertEnumToSelectList(enumType, selected, useIntValue, includeBlank);

            return(htmlHelper.DropDownListFor(expression, list, htmlAttributes));
        }