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);
        }
Пример #3
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);
        }