question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

get unobtrusive validation html attributes by ViewModelType and property name

See original GitHub issue

I need to get the unobtrusive validation html attributes (data-val-required=“msg”, data-val-length, etc.) Ideally there would be a method like this: IDictionary<string, string> dict = GetUnobtrusiveValidationAttributes<TViewModel>(propName);

I would call this inside an html helper (IHtmlHelper extension method)

this issue is similar this one for MVC5 here https://github.com/aspnet/AspNetWebStack/issues/296 where I was able to find a solution, not perfect but it works.

so is this possible in ASP.net Core ?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
pranavkmcommented, Mar 24, 2021

Something like:

ModelMetadaProvider modelMetadataProvider = /* from DI */
ValidationHtmlAttributeProvider attributeProvider = /* from DI */
var modelExplorer = modelMetadataProvider.GetModelExplorerForType(typeof(TModel));

foreach (var prop in typeof(TModel).GetProperties())
{
    var propertyExplorer = modelExplorer.GetExplorerForProperty(property.Name);
    var clientAttributes = new Dictionary<string, string>();
    
    attributeProvider.AddValidationAttributes(viewContext, propertyExplorer, clientAttributes);
}

Note that I wrote this in notepad, so I’m not entirely sure it compiles

0reactions
msftbot[bot]commented, Mar 26, 2021

This issue has been resolved and has not had any activity for 1 day. It will be closed for housekeeping purposes.

See our Issue Management Policies for more information.

Read more comments on GitHub >

github_iconTop Results From Across the Web

get unobtrusive validation attributes by ViewModel Type
I tried doing: var html2 = new HtmlHelper<TModel>(html.ViewContext, html.ViewDataContainer) and using html2 instead of html but it won't help.
Read more >
Unobtrusive Validation - ASP.NET MVC Demystified
Unobtrusive Validation allows us to take the already-existing validation attributes and use them client-side to make our user experience that ...
Read more >
Model validation in ASP.NET Core MVC and Razor Pages
Validation attributes let you specify validation rules for model properties. The following example from the sample app shows a model class ...
Read more >
Tag Helpers in forms in ASP.NET Core
The Input Tag Helper · Generates the id and name HTML attributes for the expression name specified in the asp-for attribute. · Sets...
Read more >
MVC ValidationAttribute ErrorMessage populated at runtime
I am working on a project using EPiServer 7 and MVC 4. One of the nice features in MVC is ValidationAttributes in the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found