Ejemplo n.º 1
0
        /// <inheritdoc />
        public IHtmlContent Label(string expression, string labelText, object htmlAttributes)
        {
            var modelExplorer = ExpressionMetadataProvider.FromStringExpression(expression, ViewData, MetadataProvider);

            return(GenerateLabel(
                       modelExplorer,
                       expression,
                       labelText,
                       htmlAttributes));
        }
Ejemplo n.º 2
0
        /// <inheritdoc />
        public IHtmlContent Editor(
            string expression,
            string templateName,
            string htmlFieldName,
            object additionalViewData)
        {
            var modelExplorer = ExpressionMetadataProvider.FromStringExpression(expression, ViewData, MetadataProvider);

            return(GenerateEditor(
                       modelExplorer,
                       htmlFieldName ?? ExpressionHelper.GetExpressionText(expression),
                       templateName,
                       additionalViewData));
        }
Ejemplo n.º 3
0
        /// <inheritdoc />
        public IHtmlContent Display(
            string expression,
            string templateName,
            string htmlFieldName,
            object additionalViewData)
        {
            var metadata = ExpressionMetadataProvider.FromStringExpression(expression, ViewData, MetadataProvider);

            return(GenerateDisplay(
                       metadata,
                       htmlFieldName ?? ExpressionHelper.GetExpressionText(expression),
                       templateName,
                       additionalViewData));
        }
        public void FromStringExpression_SetsContainerAsExpected()
        {
            // Arrange
            var myModel = new TestModel {
                SelectedCategory = new Category()
            };
            var provider = new EmptyModelMetadataProvider();
            var viewData = new ViewDataDictionary <TestModel>(provider);

            viewData["Object"] = myModel;

            // Act
            var metadata = ExpressionMetadataProvider.FromStringExpression("Object.SelectedCategory",
                                                                           viewData,
                                                                           provider);

            // Assert
            Assert.Same(myModel, metadata.Container.Model);
        }
        public void FromStringExpression_GetsExpectedMetadata(
            string expression,
            ModelMetadataKind expectedKind,
            Type expectedType)
        {
            // Arrange
            var provider = new EmptyModelMetadataProvider();
            var viewData = new ViewDataDictionary <TestModel>(provider);

            // Act
            var explorer = ExpressionMetadataProvider.FromStringExpression(expression, viewData, provider);

            // Assert
            Assert.NotNull(explorer);
            Assert.NotNull(explorer.Metadata);
            Assert.Equal(expectedKind, explorer.Metadata.MetadataKind);
            Assert.Equal(expectedType, explorer.ModelType);
            Assert.Null(explorer.Model);
        }
Ejemplo n.º 6
0
        /// <inheritdoc />
        public IHtmlContent TextArea(string expression, string value, int rows, int columns, object htmlAttributes)
        {
            var modelExplorer = ExpressionMetadataProvider.FromStringExpression(expression, ViewData, MetadataProvider);

            if (value != null)
            {
                // As a special case we allow treating a string value as a model of arbitrary type.
                // So pass through the string representation, even though the ModelMetadata might
                // be for some other type.
                //
                // We do this because thought we're displaying something as a string, we want to have
                // the right set of validation attributes.
                modelExplorer = new ModelExplorer(
                    MetadataProvider,
                    modelExplorer.Container,
                    modelExplorer.Metadata,
                    value);
            }

            return(GenerateTextArea(modelExplorer, expression, rows, columns, htmlAttributes));
        }
        /// <summary>
        /// Returns a <see cref="ModelExpression"/> instance describing the given <paramref name="expression"/>.
        /// </summary>
        /// <typeparam name="TModel">The type of the <paramref name="viewData"/>'s <see cref="ViewDataDictionary{T}.Model"/>.</typeparam>
        /// <param name="viewData">The <see cref="ViewDataDictionary{TModel}"/> containing the <see cref="ViewDataDictionary{T}.Model"/>
        /// against which <paramref name="expression"/> is evaluated. </param>
        /// <param name="expression">Expression name, relative to <c>viewData.Model</c>.</param>
        /// <returns>A new <see cref="ModelExpression"/> instance describing the given <paramref name="expression"/>.</returns>
        public ModelExpression CreateModelExpression <TModel>(
            ViewDataDictionary <TModel> viewData,
            string expression)
        {
            if (viewData == null)
            {
                throw new ArgumentNullException(nameof(viewData));
            }

            if (expression == null)
            {
                throw new ArgumentNullException(nameof(expression));
            }

            var modelExplorer = ExpressionMetadataProvider.FromStringExpression(expression, viewData, _modelMetadataProvider);

            if (modelExplorer == null)
            {
                throw new InvalidOperationException(
                          Resources.FormatCreateModelExpression_NullModelMetadata(nameof(IModelMetadataProvider), expression));
            }

            return(new ModelExpression(expression, modelExplorer));
        }
Ejemplo n.º 8
0
        /// <inheritdoc />
        public string DisplayText(string expression)
        {
            var modelExplorer = ExpressionMetadataProvider.FromStringExpression(expression, ViewData, MetadataProvider);

            return(GenerateDisplayText(modelExplorer));
        }