I'm curious, why I cannot access Attributes
from the code, but it's perfectly visible in debugger?
Also seems like there's no property/field called "Attributes"
Error:
'ModelMetadata' does not contain a definition for 'Attributes' and no accessible extension method 'Attributes' accepting a first argument of type 'ModelMetadata' could be found (are you missing a using directive or an assembly reference?)
Code:
using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.ViewFeatures.Internal;
using System;
using System.Linq.Expressions;
namespace Project.Views
{
public static class HtmlExtensions
{
public static IHtmlContent DescriptionFor<TModel, TValue>(this IHtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression)
{
if (html == null) throw new ArgumentNullException(nameof(html));
if (expression == null) throw new ArgumentNullException(nameof(expression));
var modelExplorer = ExpressionMetadataProvider.FromLambdaExpression(expression, html.ViewData, html.MetadataProvider);
if (modelExplorer == null) throw new InvalidOperationException($"Failed to get model explorer for {ExpressionHelper.GetExpressionText(expression)}");
var resolvedDisplayName = modelExplorer.Metadata.Attributes ?? modelExplorer.Metadata.PropertyName;
return new HtmlString(resolvedDisplayName ?? string.Empty);
}
}
}