// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; using System.Linq; using System.Globalization; using Microsoft.AspNet.Mvc.ModelBinding; namespace Microsoft.AspNet.Mvc.ViewFeatures { /// /// Extension methods for . /// public static class ModelExplorerExtensions { /// /// Gets a simple display string for the property /// of . /// /// The . /// A simple display string for the model. public static string GetSimpleDisplayText(this ModelExplorer modelExplorer) { if (modelExplorer == null) { throw new ArgumentNullException(nameof(modelExplorer)); } if (modelExplorer.Metadata.SimpleDisplayProperty != null) { var propertyExplorer = modelExplorer.GetExplorerForProperty( modelExplorer.Metadata.SimpleDisplayProperty); if (propertyExplorer?.Model != null) { return propertyExplorer.Model.ToString(); } } if (modelExplorer.Model == null) { return modelExplorer.Metadata.NullDisplayText; } var stringResult = Convert.ToString(modelExplorer.Model, CultureInfo.CurrentCulture); if (stringResult == null) { return string.Empty; } if (!stringResult.Equals(modelExplorer.Model.GetType().FullName, StringComparison.Ordinal)) { return stringResult; } var firstProperty = modelExplorer.Properties.FirstOrDefault(); if (firstProperty == null) { return string.Empty; } if (firstProperty.Model == null) { return firstProperty.Metadata.NullDisplayText; } return Convert.ToString(firstProperty.Model, CultureInfo.CurrentCulture); } } }