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

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

            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());
        }
Exemplo n.º 2
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));
        }
Exemplo n.º 3
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));
        }
Exemplo n.º 4
0
        /// <summary>
        /// Creates the template view data.
        /// </summary>
        /// <param name="viewData">The view data.</param>
        /// <param name="modelExplorer">The model explorer.</param>
        /// <param name="purpose">The purpose.</param>
        /// <param name="htmlFieldName">Name of the HTML field.</param>
        /// <param name="additionalViewData">The additional view data.</param>
        protected virtual ViewDataDictionary CreateTemplateViewData(
            ViewDataDictionary viewData,
            ModelExplorer modelExplorer,
            string purpose,
            string htmlFieldName,
            object additionalViewData)
        {
            Guard.NotNull(viewData, nameof(viewData));
            Guard.NotNull(modelExplorer, nameof(modelExplorer));
            Guard.NotNullOrEmpty(purpose, nameof(purpose));

            var result = new ViewDataDictionary <object>(viewData);
            var model  = GetModel(modelExplorer);

            result.ModelExplorer = modelExplorer.GetExplorerForModel(model);
            result.TemplateInfo.FormattedModelValue = GetFormattedModelValue(result.ModelExplorer, purpose);
            result.TemplateInfo.HtmlFieldPrefix     = viewData.TemplateInfo.GetFullHtmlFieldName(htmlFieldName);

            foreach (var kvp in HtmlHelper.ObjectToDictionary(additionalViewData))
            {
                viewData[kvp.Key] = kvp.Value;
            }

            return(result);
        }
Exemplo n.º 5
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.º 6
0
        private TagBuilder GenerateTextBox(
            ModelExplorer modelExplorer,
            string inputTypeHint,
            string inputType,
            IDictionary <string, object> htmlAttributes)
        {
            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))
                {
                    // throw new System.NotImplementedException("GetExplorerForModel => FormatWeekHelper");
                    modelExplorer = modelExplorer.GetExplorerForModel(FormatWeekHelper.GetFormattedWeek(modelExplorer));
                }
                else
                {
                    format = GetFormat(modelExplorer, inputTypeHint, inputType);
                }
            }

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

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

            return(Generator.GenerateTextBox(
                       ViewContext,
                       modelExplorer,
                       For.Name,
                       modelExplorer.Model,
                       format,
                       htmlAttributes));
        }
Exemplo n.º 7
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());
    }
Exemplo n.º 8
0
        public async Task <IHtmlContent> Invoke(ModelExpression expression, O options, ContextualizedHelpers helpers, string overridePrefix = null, object data = null)
        {
            if (expression == null)
            {
                throw new ArgumentNullException(nameof(expression));
            }
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            ModelExplorer model = expression.ModelExplorer;

            if (Type == TemplateType.Partial)
            {
                if (helpers == null)
                {
                    throw new ArgumentNullException(nameof(helpers));
                }
                var h      = helpers.Html;
                var origVd = helpers.Context.ViewData;
                if (h == null)
                {
                    throw new ArgumentNullException(nameof(h));
                }
                var fatherPrefix = (origVd[RenderingScope.Field] as RenderingScope)?.FatherPrefix;
                var vd           = new ViewDataDictionary <object>(origVd);
                vd.Model         = model.Model;
                vd.ModelExplorer = model.GetExplorerForModel(model.Model);
                if (overridePrefix != null)
                {
                    vd.TemplateInfo.HtmlFieldPrefix = combinePrefixes(overridePrefix, expression.Name);
                }
                else
                {
                    vd.TemplateInfo.HtmlFieldPrefix = origVd.GetFullHtmlFieldName(combinePrefixes(fatherPrefix, expression.Name));
                }
                vd["Options"] = options;
                vd["ContextualizedHelpers"] = helpers;
                vd["LocalizerFactory"]      = helpers.LocalizerFactory;
                if (data != null)
                {
                    vd["ExtraData"] = data;
                }
                vd.Remove(RenderingScope.Field);
                return(await h.PartialAsync(TemplateName, model.Model, vd));
            }
            else if (Type == TemplateType.ViewComponent)
            {
                if (helpers == null)
                {
                    throw new ArgumentNullException(nameof(helpers));
                }
                var origVd       = helpers.Context.ViewData;
                var fatherPrefix = (origVd[RenderingScope.Field] as RenderingScope)?.FatherPrefix;
                if (data != null)
                {
                    return(await helpers.Component.InvokeAsync(TemplateName, new
                    {
                        model = model.Model,
                        options = options,
                        prefix = overridePrefix != null ? combinePrefixes(overridePrefix, expression.Name) : origVd.GetFullHtmlFieldName(combinePrefixes(fatherPrefix, expression.Name)),
                        modelState = origVd.ModelState,
                        extraData = data
                    }));
                }
                else
                {
                    return(await helpers.Component.InvokeAsync(TemplateName, new {
                        model = model.Model,
                        options = options,
                        prefix = overridePrefix != null ? combinePrefixes(overridePrefix, expression.Name) : origVd.GetFullHtmlFieldName(combinePrefixes(fatherPrefix, expression.Name)),
                        modelState = origVd.ModelState
                    }));
                }
            }
            else if (Type == TemplateType.InLine)
            {
                var res = helpers.GetCachedTemplateResult(this);
                if (res != null)
                {
                    return(res);
                }
                using (await Lock.LockAsync())
                {
                    originalContext.HttpContext = helpers.CurrentHttpContext;

                    var origVd = originalContext.ViewData;
                    using (new TagHelpersProviderContext(OriginalProvider, originalContext))
                    {
                        using (new RenderingScope(
                                   expression.Model,
                                   overridePrefix != null ? combinePrefixes(overridePrefix, expression.Name) : helpers.Context.ViewData.GetFullHtmlFieldName(expression.Name),
                                   origVd,
                                   options))
                        {
                            return(FTemplate(model.Model, default(O), helpers));
                        }
                    }
                }
            }
            else
            {
                if (helpers == null)
                {
                    throw new ArgumentNullException(nameof(helpers));
                }
                var origVd = helpers.Context.ViewData;
                using (new RenderingScope(
                           expression.Model,
                           origVd,
                           expression.Name,
                           options))
                {
                    return(FTemplate(model.Model, options, helpers));
                }
            }
        }