Click here to Skip to main content
15,892,965 members
Home / Discussions / C#
   

C#

 
AnswerRe: Want to change the Validation attribute at runtime ASP.Net MVC Pin
Richard Deeming3-Aug-18 10:21
mveRichard Deeming3-Aug-18 10:21 
GeneralRe: Want to change the Validation attribute at runtime ASP.Net MVC Pin
indian1433-Aug-18 11:16
indian1433-Aug-18 11:16 
GeneralRe: Want to change the Validation attribute at runtime ASP.Net MVC Pin
Richard Deeming3-Aug-18 12:14
mveRichard Deeming3-Aug-18 12:14 
GeneralRe: Want to change the Validation attribute at runtime ASP.Net MVC Pin
indian1433-Aug-18 12:48
indian1433-Aug-18 12:48 
GeneralRe: Want to change the Validation attribute at runtime ASP.Net MVC Pin
Richard Deeming6-Aug-18 2:05
mveRichard Deeming6-Aug-18 2:05 
GeneralRe: Want to change the Validation attribute at runtime ASP.Net MVC Pin
indian1436-Aug-18 6:39
indian1436-Aug-18 6:39 
GeneralRe: Want to change the Validation attribute at runtime ASP.Net MVC Pin
Richard Deeming6-Aug-18 7:15
mveRichard Deeming6-Aug-18 7:15 
GeneralRe: Want to change the Validation attribute at runtime ASP.Net MVC Pin
indian1436-Aug-18 7:32
indian1436-Aug-18 7:32 
But still the attributes value in IEnumerable<attribute> attributes coming as null when I already have one Attribute on that class and when I try to expan the attributes collection object, it shows me "Enumeration yielded no results" in the end.
    public class LookupTable
    {
        public int Id { get; set; }

        [Required(ErrorMessage = "Code is required.")]<br />
        public string Code { get; set; }

        [Required(ErrorMessage = "Description is required.")]
        public string Description { get; set; }
        public int? ForeignKeyId { get; set; }
        public string ForeignKeyValue { get; set; }
        public DateTime? CreatedDate {get;set;} 
        public string CreatedBy {get;set;}       
        public int CodeLength { get; set; }
    }

Do I need to add anymore Attributes to the Code Property?, what am I missing here my friend?

Even after I changed my function to have Container instead of Model, still I get null at metadata.PropertyName

Always metadata.PropertyName is coming as null irrespective of any changes I do at if (metadata.PropertyName == GetPropertyName(() => model.Code) && (model.CodeLength > 0))
in the below function and attributes is also coming as null.
        protected override IEnumerable<System.Web.Mvc.ModelValidator> GetValidators(System.Web.Mvc.ModelMetadata metadata, ControllerContext context, IEnumerable<Attribute> attributes)
        { 
            if (metadata.Model is LookupTable)
            {
                LookupTable model = metadata.Container as LookupTable;                

                if (metadata.PropertyName == GetPropertyName(() => model.Code) && (model.CodeLength > 0))
                {
                    var newAttributes = new List<Attribute>(attributes);
                    var stringLength = newAttributes.OfType<StringLengthAttribute>().FirstOrDefault();
                    if (stringLength != null)
                    {
                        newAttributes.Remove(stringLength);

                        if (model.CodeLength != 0)
                        {
                            newAttributes.Add(new StringLengthAttribute(model.CodeLength)
                            {
                                MinimumLength = model.CodeLength,
                                ErrorMessage = @"The field {{0}} length must be at least {model.CodeLength}."
                            });
                        }

                        attributes = newAttributes;
                    }
                }
            }            

            return base.GetValidators(metadata, context, attributes);
        }

Thanks,

Abdul Aleem

"There is already enough hatred in the world lets spread love, compassion and affection."
GeneralRe: Want to change the Validation attribute at runtime ASP.Net MVC Pin
Richard Deeming6-Aug-18 9:16
mveRichard Deeming6-Aug-18 9:16 
GeneralRe: Want to change the Validation attribute at runtime ASP.Net MVC Pin
indian1436-Aug-18 10:12
indian1436-Aug-18 10:12 
GeneralRe: Want to change the Validation attribute at runtime ASP.Net MVC Pin
Richard Deeming7-Aug-18 1:56
mveRichard Deeming7-Aug-18 1:56 
QuestionWhen inputting data form main one other always on form main ? Pin
Member 24584672-Aug-18 21:29
Member 24584672-Aug-18 21:29 
AnswerRe: When inputting data form main one other always on form main ? Pin
Richard MacCutchan2-Aug-18 21:40
mveRichard MacCutchan2-Aug-18 21:40 
AnswerRe: When inputting data form main one other always on form main ? Pin
OriginalGriff2-Aug-18 21:45
mveOriginalGriff2-Aug-18 21:45 
GeneralRe: When inputting data form main one other always on form main ? Pin
Member 24584675-Aug-18 16:42
Member 24584675-Aug-18 16:42 
GeneralRe: When inputting data form main one other always on form main ? Pin
OriginalGriff5-Aug-18 19:35
mveOriginalGriff5-Aug-18 19:35 
AnswerRe: When inputting data form main one other always on form main ? Pin
BillWoodruff3-Aug-18 2:17
professionalBillWoodruff3-Aug-18 2:17 
QuestionC# Pin
Jeremiah Okoduwa2-Aug-18 0:51
Jeremiah Okoduwa2-Aug-18 0:51 
AnswerRe: C# Pin
Daniel Pfeffer2-Aug-18 1:12
professionalDaniel Pfeffer2-Aug-18 1:12 
AnswerRe: C# Pin
Nathan Minier2-Aug-18 1:14
professionalNathan Minier2-Aug-18 1:14 
AnswerRe: C# Pin
OriginalGriff2-Aug-18 1:19
mveOriginalGriff2-Aug-18 1:19 
AnswerRe: C# Pin
Richard MacCutchan2-Aug-18 1:33
mveRichard MacCutchan2-Aug-18 1:33 
AnswerRe: C# Pin
BillWoodruff3-Aug-18 2:23
professionalBillWoodruff3-Aug-18 2:23 
QuestionCombining TreeView and Canvas in WPF ( MVVM ) Pin
Kenneth Haugland30-Jul-18 21:50
mvaKenneth Haugland30-Jul-18 21:50 
AnswerRe: Combining TreeView and Canvas in WPF ( MVVM ) Pin
Pete O'Hanlon30-Jul-18 23:47
mvePete O'Hanlon30-Jul-18 23:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.