简体   繁体   中英

How to get HTML annotations from DataAnnotations from a class that is not part of a model?

How do I get hold of the html annotations (data-val="true" data-val-required="Required") from a class that is not part of the whole PAGE model.

I use the class to populate a GridView that has it's values stored in a Session Variable.

When I use TextBoxFor(x=>x.Name) for the gridview, I do not get the html annotations generated like in the rest of the page.

The code below does not help me at all.

var metadata = ModelMetadata.FromLambdaExpression(expression,helper.ViewData); 
string prefix = ExpressionHelper.GetExpressionText(expression);

You should not make any calls to Session object from inside the view. Instead you should express this data as part of your viewmodel and fill it in controller.

In fact, you should not use any data that's not part of your viewmodel.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM