I have been trying to add validation attributes dynamically at runtime and all works ok.
I next tried to add a DisplayAttribute at runtime hooking into the same
public class CustomModelValidatorProvider : DataAnnotationsModelValidatorProvider
{
protected override IEnumerable<ModelValidator> GetValidators(ModelMetadata metadata, ControllerContext context, IEnumerable<Attribute> attributes)
{
List<Attribute> newAttributes = new List<Attribute>(attributes);
if (!string.IsNullOrWhiteSpace(metadata.PropertyName) && metadata.PropertyName == "LastName")
{
//add a new one -WORKS!
StringLengthAttribute attr = new StringLengthAttribute(25);
attr.MinimumLength = 2;
newAttributes.Add(attr);
}
if (!string.IsNullOrWhiteSpace(metadata.PropertyName) && metadata.PropertyName == "City")
{
//add a new one - does not work
DisplayAttribute attr = new DisplayAttribute();
attr.Name = "The Big City";
newAttributes.Add(attr);
}
return base.GetValidators(metadata, context, newAttributes);
}
}
Either consider using MVC 3 or .....may try to handle DisplayAttribute from scratch. What do you would like to do with the display attribute? If it is a quite self contained use one can try to handle it someway...Let me know.
however, because the attribute i am adding is not strictly a validation attribute, it does not appear to work here is my code:
Display attribute is not an validation attribute, Try something like,
if (!string.IsNullOrWhiteSpace(metadata.PropertyName) && metadata.PropertyName == "City")
{ metadata.DisplayName = "The Big City";
}
"And whoever is removed away from the Fire and admitted to Paradise, he indeed is successful." (The Holy Quran)
Excellent Windows VPS Hosting Imran Baloch MVP, MVB, MCP, MCTS, MCPD
The point is that DisplayAttribute is not a validation attribute, so returning it in the list of validator doesn't make sense.
The only thing is trying to give a value directly to the metadata property displayName as suggested by Irman. However, also this way I don'tknow if it works when done in that point.
If you don't have luck this way, try to set the value directly when metadata is going to be passed to the view, defining a custom filter for your action.
again - yes i said this in a slighlty different way, but is did say it in my very first post. in other words i discovered my mistake the hard way myself...
so, moving forward - not sure how else to ask this but ....
is there any other override i can hook into to carry out this kind of dynamic DISPLAY attribute task ?
snuggles
Member
20 Points
49 Posts
Dynamic Attributes - DisplayAttribute
Nov 24, 2010 05:50 AM|LINK
I have been trying to add validation attributes dynamically at runtime and all works ok.
I next tried to add a DisplayAttribute at runtime hooking into the same
public class CustomModelValidatorProvider : DataAnnotationsModelValidatorProvider { protected override IEnumerable<ModelValidator> GetValidators(ModelMetadata metadata, ControllerContext context, IEnumerable<Attribute> attributes) { List<Attribute> newAttributes = new List<Attribute>(attributes); if (!string.IsNullOrWhiteSpace(metadata.PropertyName) && metadata.PropertyName == "LastName") { //add a new one -WORKS! StringLengthAttribute attr = new StringLengthAttribute(25); attr.MinimumLength = 2; newAttributes.Add(attr); } if (!string.IsNullOrWhiteSpace(metadata.PropertyName) && metadata.PropertyName == "City") { //add a new one - does not work DisplayAttribute attr = new DisplayAttribute(); attr.Name = "The Big City"; newAttributes.Add(attr); } return base.GetValidators(metadata, context, newAttributes); } }attribute
francesco ab...
All-Star
20912 Points
3279 Posts
Re: Dynamic Attributes - DisplayAttribute
Nov 24, 2010 08:50 AM|LINK
The Point is that MVC 2 doesn't support the DisplayAttribute. This support has been introduced with MVC 3, see here: http://weblogs.asp.net/scottgu/archive/2010/07/27/introducing-asp-net-mvc-3-preview-1.aspx
Either consider using MVC 3 or .....may try to handle DisplayAttribute from scratch. What do you would like to do with the display attribute? If it is a quite self contained use one can try to handle it someway...Let me know.
Mvc Controls Toolkit | Data Moving Plug-in Videos
snuggles
Member
20 Points
49 Posts
Re: Dynamic Attributes - DisplayAttribute
Nov 24, 2010 10:57 PM|LINK
but i am using MVC3...still not sure where to hook into the metamodel...
imran_ku07
All-Star
45815 Points
7698 Posts
MVP
Re: Dynamic Attributes - DisplayAttribute
Nov 25, 2010 04:18 AM|LINK
Display attribute is not an validation attribute, Try something like,
if (!string.IsNullOrWhiteSpace(metadata.PropertyName) && metadata.PropertyName == "City")
{
metadata.DisplayName = "The Big City";
}
Excellent Windows VPS Hosting
Imran Baloch MVP, MVB, MCP, MCTS, MCPD
francesco ab...
All-Star
20912 Points
3279 Posts
Re: Dynamic Attributes - DisplayAttribute
Nov 25, 2010 08:28 AM|LINK
The point is that DisplayAttribute is not a validation attribute, so returning it in the list of validator doesn't make sense.
The only thing is trying to give a value directly to the metadata property displayName as suggested by Irman. However, also this way I don'tknow if it works when done in that point.
If you don't have luck this way, try to set the value directly when metadata is going to be passed to the view, defining a custom filter for your action.
Mvc Controls Toolkit | Data Moving Plug-in Videos
snuggles
Member
20 Points
49 Posts
Re: Dynamic Attributes - DisplayAttribute
Nov 25, 2010 11:15 PM|LINK
hi,
the point that it is not a validation attribute was stated by me in my original post - so nothing new there...(not sure why it's repeated back to me)
i'm left with my original question still unanswered and i'm guessing i'll have to investigate further - thanks for the replies.
p.s imran, if you tested that you'd know that does not work...
bradwils
Contributor
5779 Points
691 Posts
Microsoft
Re: Dynamic Attributes - DisplayAttribute
Nov 25, 2010 11:26 PM|LINK
The point is that you're inside of a VALIDATION provider. DisplayAttribute is not a VALIDATION attribute.
If you want to influence the things that the DisplayAttribute influences, you need to write a METADATA provider.
snuggles
Member
20 Points
49 Posts
Re: Dynamic Attributes - DisplayAttribute
Nov 25, 2010 11:32 PM|LINK
again - yes i said this in a slighlty different way, but is did say it in my very first post. in other words i discovered my mistake the hard way myself...
so, moving forward - not sure how else to ask this but ....
is there any other override i can hook into to carry out this kind of dynamic DISPLAY attribute task ?
bradwils
Contributor
5779 Points
691 Posts
Microsoft
Re: Dynamic Attributes - DisplayAttribute
Nov 25, 2010 11:46 PM|LINK
The only way to do it is to write a model metadata provider. There is no other way.
snuggles
Member
20 Points
49 Posts
Re: Dynamic Attributes - DisplayAttribute
Nov 25, 2010 11:49 PM|LINK
ok, thanks for that - at least there is a way.
cheers.