예제 #1
0
        private TagBuilder GenerateTextBox(ModelExplorer modelExplorer, string inputTypeHint, string inputType)
        {
            var format = Format;

            if (string.IsNullOrEmpty(format))
            {
                if (!modelExplorer.Metadata.HasNonDefaultEditFormat &&
                    string.Equals("week", inputType, StringComparison.OrdinalIgnoreCase) &&
                    (modelExplorer.Model is DateTime || modelExplorer.Model is DateTimeOffset))
                {
                    modelExplorer = modelExplorer.GetExplorerForModel(FormatWeekHelper.GetFormattedWeek(modelExplorer));
                }
                else
                {
                    format = GetFormat(modelExplorer, inputTypeHint, inputType);
                }
            }
            var htmlAttributes = new Dictionary <string, object>
            {
                { "type", inputType }
            };

            if (string.Equals(inputType, "file") && string.Equals(inputTypeHint, TemplateRenderer.IEnumerableOfIFormFileName))
            {
                htmlAttributes["multiple"] = "multiple";
            }

            return(Generator.GenerateTextBox(
                       ViewContext,
                       modelExplorer,
                       For.Name,
                       value: modelExplorer.Model,
                       format: format,
                       htmlAttributes: htmlAttributes));
        }
예제 #2
0
        private TagBuilder GenerateTextField(ModelExplorer modelExplorer, string?inputTypeHint, string inputType, IDictionary <string, object>?htmlAttributes)
        {
            var format = Format;

            if (string.IsNullOrEmpty(format))
            {
                if (modelExplorer.Metadata.HasNonDefaultEditFormat != true &&
                    string.Equals("week", inputType, StringComparison.OrdinalIgnoreCase) &&
                    (modelExplorer.Model is DateTime || modelExplorer.Model is DateTimeOffset))
                {
                    modelExplorer = modelExplorer.GetExplorerForModel(FormatWeekHelper.GetFormattedWeek(modelExplorer));
                }

                else
                {
                    format = GetFormat(modelExplorer, inputTypeHint, inputType);
                }
            }

            if (htmlAttributes is null)
            {
                htmlAttributes = new Dictionary <string, object>(StringComparer.OrdinalIgnoreCase);
            }

            htmlAttributes["type"] = inputType;
            return(Generator.GenerateTextField(
                       ViewContext,
                       modelExplorer,
                       InputType.Text,
                       For.Name,
                       modelExplorer.Model,
                       format,
                       htmlAttributes,
                       DesignSystem));
        }
예제 #3
0
    public void GetFormattedWeek_ReturnsExpectedFormattedValue(int year, int month, int day, string expectedOutput)
    {
        // Arrange
        var detailsProvider = new DefaultCompositeMetadataDetailsProvider(
            Enumerable.Empty <IMetadataDetailsProvider>());
        var key   = ModelMetadataIdentity.ForType(typeof(DateTime));
        var cache = new DefaultMetadataDetails(key, new ModelAttributes(
                                                   Array.Empty <object>(),
                                                   Array.Empty <object>(),
                                                   Array.Empty <object>()));

        var provider      = new EmptyModelMetadataProvider();
        var metadata      = new DefaultModelMetadata(provider, detailsProvider, cache);
        var model         = new DateTime(year, month, day);
        var modelExplorer = new ModelExplorer(provider, metadata, model);

        // Act
        var formattedValue = FormatWeekHelper.GetFormattedWeek(modelExplorer);

        // Assert
        Assert.Equal(expectedOutput, formattedValue);
    }
예제 #4
0
    public IHtmlContent Build()
    {
        if (_metadata.ConvertEmptyStringToNull && string.Empty.Equals(_model))
        {
            _model = null;
        }

        // 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);
        }

        // Create VDD of type object so any model type is allowed.
        var viewData = new ViewDataDictionary <object>(_viewData);

        // Create a new ModelExplorer in order to preserve the model metadata of the original _viewData even
        // though _model may have been reset to null. Otherwise we might lose track of the model type /property.
        viewData.ModelExplorer = _modelExplorer.GetExplorerForModel(_model);

        var formatString = _readOnly ?
                           viewData.ModelMetadata.DisplayFormatString :
                           viewData.ModelMetadata.EditFormatString;

        var formattedModelValue = _model;

        if (_model == null)
        {
            if (_readOnly)
            {
                formattedModelValue = _metadata.NullDisplayText;
            }
        }
        else if (!string.IsNullOrEmpty(formatString))
        {
            formattedModelValue = string.Format(CultureInfo.CurrentCulture, formatString, _model);
        }
        else if ((string.Equals("week", _templateName, StringComparison.OrdinalIgnoreCase) ||
                  string.Equals("week", viewData.ModelMetadata.DataTypeName, StringComparison.OrdinalIgnoreCase)))
        {
            // "week" is a new HTML5 input type that only will be rendered in Rfc3339 mode
            formattedModelValue = FormatWeekHelper.GetFormattedWeek(_modelExplorer);
        }
        else if (viewData.ModelMetadata.IsEnum && _model is Enum modelEnum)
        {
            // Cover the case where the model is an enum and we want the string value of it
            var value       = modelEnum.ToString("d");
            var enumGrouped = viewData.ModelMetadata.EnumGroupedDisplayNamesAndValues;
            Debug.Assert(enumGrouped != null);
            foreach (var kvp in enumGrouped)
            {
                if (kvp.Value == value)
                {
                    // Creates a ModelExplorer with the same Metadata except that the Model is a string instead of an Enum
                    formattedModelValue = kvp.Key.Name;
                    break;
                }
            }
        }

        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,
            _bufferScope,
            _viewContext,
            viewData,
            _templateName,
            _readOnly);

        return(templateRenderer.Render());
    }