how to check null value of Integer type field in ASP.NET MVC view?

Posted by Vikas on Stack Overflow See other posts from Stack Overflow or by Vikas
Published on 2009-04-02T13:28:29Z Indexed on 2010/04/08 5:43 UTC
Read the original article Hit count: 254

Filed under:

Hi,

I have integer type field in database which is having property "Not Null".

when i create a view & do a validation, if i left that field blank, it will consider it as 0 so i can not compare it with 0 because if someone insert a value 0 then it will be considered as error!

one another problem is that i am using Model error as described in the book "ASP.NET MVC 1.0" @ Scott Gu blog. And I am checking the value in partial class of object (created by LINQ-To-SQL). i.e

public partial class Person
{
    public bool IsValid
    {
        get { return (GetRuleViolations().Count() == 0); }
    }
    public IEnumerable<RuleViolation> GetRuleViolations()
    {
        if (String.IsNullOrEmpty(Name))
            yield return new RuleViolation("Name is Required", "Name");
        if (Age == 0)
            yield return new RuleViolation("Age is Required", "Age");
        yield break;
    }
    partial void OnValidate(ChangeAction action)
    {
        if (!IsValid)
            throw new ApplicationException("Rule violations prevent saving");
    }
}

There is also problem with range.

Like in database if i declared as smallint i.e. short in c#, now if i exceed that range then it gives error as "A Value is reguired".

so finally is there any best way for validation in ASP.NET MVC?

© Stack Overflow or respective owner

Related posts about asp.net-mvc