Exemplo n.º 1
0
        public IHtmlContent Build()
        {
            if (_metadata.ConvertEmptyStringToNull && string.Empty.Equals(_model))
            {
                _model = null;
            }

            var formattedModelValue = _model;

            if (_model == null && _readOnly)
            {
                formattedModelValue = _metadata.NullDisplayText;
            }

            var formatString = _readOnly ? _metadata.DisplayFormatString : _metadata.EditFormatString;

            if (_model != null && !string.IsNullOrEmpty(formatString))
            {
                formattedModelValue = string.Format(CultureInfo.CurrentCulture, formatString, _model);
            }

            // Normally this shouldn't happen, unless someone writes their own custom Object templates which
            // don't check to make sure that the object hasn't already been displayed
            if (_viewData.TemplateInfo.Visited(_modelExplorer))
            {
                return(HtmlString.Empty);
            }

            // We need to copy the ModelExplorer to copy the model metadata. Otherwise we might
            // lose track of the model type/property. Passing null here explicitly, because
            // this might be a typed VDD, and the model value might not be compatible.
            // Create VDD of type object so it retains the correct metadata when the model type is not known.
            var viewData = new ViewDataDictionary <object>(_viewData, model: null);

            // We're setting ModelExplorer in order to preserve the model metadata of the original
            // _viewData even though _model may be null.
            viewData.ModelExplorer = _modelExplorer.GetExplorerForModel(_model);

            viewData.TemplateInfo.FormattedModelValue = formattedModelValue;
            viewData.TemplateInfo.HtmlFieldPrefix     = _viewData.TemplateInfo.GetFullHtmlFieldName(_htmlFieldName);

            if (_additionalViewData != null)
            {
                foreach (var kvp in HtmlHelper.ObjectToDictionary(_additionalViewData))
                {
                    viewData[kvp.Key] = kvp.Value;
                }
            }

            var visitedObjectsKey = _model ?? _modelExplorer.ModelType;

            viewData.TemplateInfo.AddVisited(visitedObjectsKey);

            var templateRenderer = new TemplateRenderer(_viewEngine, _viewContext, viewData, _templateName, _readOnly);

            return(templateRenderer.Render());
        }
Exemplo n.º 2
0
        public void GetTypeNames_ReturnsExpectedResults(Type fieldType, string[] expectedResult)
        {
            // Arrange
            var metadataProvider = new TestModelMetadataProvider();
            var metadata         = metadataProvider.GetMetadataForType(fieldType);

            // Act
            var typeNames = TemplateRenderer.GetTypeNames(metadata, fieldType);

            // Assert
            var collectionAssertions = expectedResult.Select <string, Action <string> >(expected =>
                                                                                        actual => Assert.Equal(expected, actual));

            Assert.Collection(typeNames, collectionAssertions.ToArray());
        }