Rectangle 27 0

c How to do Integer model validation in asp.net mvc 2?


[DisplayName("Square meters")]
    [PosNumberNoZero(ErrorMessage = "need a positive number, bigger than 0")]
    public string squaremeters { get; set; }
public class PosNumberAttribute : ValidationAttribute {
    public override bool IsValid(object value) {
        if (value == null) {
            return true;
        }
        int getal;
        if (int.TryParse(value.ToString(), out getal)) {

            if (getal >= 0)
                return true;
        }
        return false;
    }
}
public class PosNumberNoZeroAttribute : ValidationAttribute {
    public override bool IsValid(object value) {
        if (value == null) {
            return true;
        }
        int getal;
        if (int.TryParse(value.ToString(), out getal)) {

            if (getal == 0)
                return false;

            if (getal > 0)
                return true;
        }
        return false;

    }
}

Why not use decimal.TryParse or something? In that way, you can support more number types than just integers

can you post the object you are wanting to make?

in the same file

no problem. i hope you get it to work for yourself ;)

sorry. for the trouble I should have known that. Just had a brain fart

with a flat version i mean all datetimes and ints are stings in the flat version.

yes, it is, but you will have to make a flat version of the object you are wanting to create, because the validation with attributes only runs AFTER MVC has converted your data into the model. which, when your value is an int, will fail to validate if the user did not enter an int, and you will get a MVC error message in stead of your errormessage.

Note
Rectangle 27 0

c How to do Integer model validation in asp.net mvc 2?


[DisplayName("Square meters")]
    [PosNumberNoZero(ErrorMessage = "need a positive number, bigger than 0")]
    public string squaremeters { get; set; }
public class PosNumberAttribute : ValidationAttribute {
    public override bool IsValid(object value) {
        if (value == null) {
            return true;
        }
        int getal;
        if (int.TryParse(value.ToString(), out getal)) {

            if (getal >= 0)
                return true;
        }
        return false;
    }
}
public class PosNumberNoZeroAttribute : ValidationAttribute {
    public override bool IsValid(object value) {
        if (value == null) {
            return true;
        }
        int getal;
        if (int.TryParse(value.ToString(), out getal)) {

            if (getal == 0)
                return false;

            if (getal > 0)
                return true;
        }
        return false;

    }
}

Why not use decimal.TryParse or something? In that way, you can support more number types than just integers

can you post the object you are wanting to make?

in the same file

no problem. i hope you get it to work for yourself ;)

sorry. for the trouble I should have known that. Just had a brain fart

with a flat version i mean all datetimes and ints are stings in the flat version.

yes, it is, but you will have to make a flat version of the object you are wanting to create, because the validation with attributes only runs AFTER MVC has converted your data into the model. which, when your value is an int, will fail to validate if the user did not enter an int, and you will get a MVC error message in stead of your errormessage.

Note