public string 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(string.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. var viewData = new ViewDataDictionary(_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()); }
public string Build() { if (_metadata.ConvertEmptyStringToNull && string.Empty.Equals(_metadata.Model)) { _metadata.Model = null; } var formattedModelValue = _metadata.Model; if (_metadata.Model == null && _readOnly) { formattedModelValue = _metadata.NullDisplayText; } var formatString = _readOnly ? _metadata.DisplayFormatString : _metadata.EditFormatString; if (_metadata.Model != null && !string.IsNullOrEmpty(formatString)) { formattedModelValue = string.Format(CultureInfo.CurrentCulture, formatString, _metadata.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(_metadata)) { return(string.Empty); } var viewData = new ViewDataDictionary(_viewData) { Model = _metadata.Model, ModelMetadata = _metadata }; 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 = _metadata.Model ?? _metadata.RealModelType; viewData.TemplateInfo.AddVisited(visitedObjectsKey); var templateRenderer = new TemplateRenderer(_viewEngine, _viewContext, viewData, _templateName, _readOnly); return(templateRenderer.Render()); }