Пример #1
0
        private static IEnumerable GetDataSource(SelectListSourceAttribute attribute, object viewModel)
        {
            var dataSourceProperty = viewModel.GetType().GetProperty(attribute.DataSourceName);

            if (dataSourceProperty == null)
            {
                throw new ArgumentException(
                          String.Format(
                              "The type '{0}' does not contain a property named '{1}' as indicated by the SelectListSourceAttribute.",
                              viewModel.GetType().FullName, attribute.DataSourceName));
            }

            var dataSource = dataSourceProperty.GetValue(viewModel);

            if (dataSource == null)
            {
                throw new ArgumentException(string.Format("The data source property '{0}' cannot be null.", attribute.DataSourceName));
            }
            if (dataSource as IEnumerable == null)
            {
                throw new ArgumentException(String.Format("In order to be utilized as a SelectListSource, the property '{0}' must implement IEnumerable.", attribute.DataSourceName));
            }

            return(dataSource as IEnumerable);
        }
        public void WhenOptionalLabelIsNotSupplied_NoLabelItemIsAdded()
        {
            // Arrange
            var classUnderTest = new SelectListSourceAttribute("DataSourceProp", "Id", "Text");
            var sourceItems    = Builder <SelectListItemObject> .CreateListOfSize(3).Build();

            // Act
            var results = classUnderTest.BuildSelectListItems(sourceItems).ToList();

            // Assert
            Assert.AreEqual(sourceItems.Count, results.Count);
        }
        public void WhenOptionalLabelIsEmptyString_LabelItemIsAdded()
        {
            // Arrange
            var classUnderTest = new SelectListSourceAttribute("DataSourceProp", "Id", "Text", "");
            var sourceItems    = Builder <SelectListItemObject> .CreateListOfSize(3).Build();

            // Act
            var results = classUnderTest.BuildSelectListItems(sourceItems).ToList();

            // Assert
            Assert.AreEqual(sourceItems.Count + 1, results.Count);
            Assert.AreEqual("", results[0].Text);
            Assert.AreEqual("", results[0].Value);
        }
Пример #4
0
        private static void BuildDataSourceAndInsertIntoViewData(object viewModel, SelectListSourceAttribute attr, PropertyInfo boundProperty, IDictionary <string, object> viewData)
        {
            if (boundProperty.PropertyType.IsClass && boundProperty.PropertyType != typeof(string))
            {
                throw new ArgumentException(
                          String.Format("The BindSelectListsAttribute does not support binding to a class objects. The property attempting to be bound is '{0}' which is of type '{1}'.",
                                        boundProperty.Name,
                                        viewModel.GetType().FullName));
            }

            var dataSource  = GetDataSource(attr, viewModel);
            var viewDataKey = BuildViewDataKey(boundProperty.Name);

            viewData[viewDataKey] = viewData[viewDataKey] ?? attr.BuildSelectListItems(dataSource);
        }