I am trying to access a model's metadata attributes to create a helper to automatically add HtmlAttributes based on DataAnnotations.
Problem is, the attributes are always empty.
I have 2 basic classes to try on an empty project:
namespace MegaInterestingProject
{
public class HomeController : Controller
{
public string Index()
{
var model = ModelMetadata.FromLambdaExpression<HomeModel, string>(x => x.User, new ViewDataDictionary<HomeModel>());
return model.Description;
}
}
public class HomeModel
{
[Required]
[MaxLength(13)]
[MinLength(11)]
[DisplayName("displayname")]
[Description("description")]
public string User { get; set; }
}
}
Here model.Description is always empty and the AdditionalValues dictionary is always empty.
Maybe something is missing here?
Here is a reference project I added on GitHub: https://github.com/erickgirard/TestHtmlAttributesHelper