Click here to Skip to main content
15,888,521 members
Home / Discussions / C#
   

C#

 
GeneralRe: SelectedIndex doesn't work with the SelectionChanged event handler but it works with the ButtonClick event handler. Pin
Richard Deeming20-Aug-18 7:52
mveRichard Deeming20-Aug-18 7:52 
QuestionWant to change the Validation attribute at runtime ASP.Net MVC Pin
indian1433-Aug-18 7:57
indian1433-Aug-18 7:57 
AnswerRe: Want to change the Validation attribute at runtime ASP.Net MVC Pin
Gerry Schmitz3-Aug-18 9:18
mveGerry Schmitz3-Aug-18 9:18 
GeneralRe: Want to change the Validation attribute at runtime ASP.Net MVC Pin
indian1433-Aug-18 10:17
indian1433-Aug-18 10:17 
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 
Hi Richard,

One last thing, I have created the method in the following way:
    public class CustomModelValidatorProvider : System.Web.Mvc.DataAnnotationsModelValidatorProvider
    {
        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.Model as LookupTable;

                if (metadata.PropertyName == GetPropertyName(() => model.CodeLength) && (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);
        }

        public string GetPropertyName<T>(Expression<Func<T>> propertyLambda)
        {
            var me = propertyLambda.Body as MemberExpression;

            if (me == null)
            {
                throw new ArgumentException("You must pass a lambda of the form: '() => Class.Property' or '() => object.Property'");
            }

            return me.Member.Name;
        }
    }
can you please let me know how can I decorate or use it on my Model class, can you please give me that example, I am asking this question because this value is coming as null: metadata.PropertyName, so I am not able to compare with property name. So any help please?

thank you very very much buddy - as I said earlier have a Wonderful weekend with family and friends buddy.

Thanks,

Abdul Aleem

"There is already enough hatred in the world lets spread love, compassion and affection."

-- modified 3-Aug-18 19:53pm.
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 
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 

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.