tags:

views:

966

answers:

4

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?

A: 

I'm not realy up to speed with the details of MVC, but isn't there a way to change the default value of age to -1? This way you have a value to check on and people can still enter 0.

Another solution could be to use a nullable int. You can do this by appending a ? to the type.

HTH.

Jonathan

Jonathan van de Veen
But if we dont enter any value then it consider 0. so we cannot check it with -1.
Vikas
I don't get what your saying here. A default value is a default value (meaning that it should have this value whenever no value was set).If you mean for the nullable type, then the value is null if nothing was set.
Jonathan van de Veen
A: 

I would validate the form on the client first. If the age is 0 then let the user know that it's not a valid age. On the server change the message, it's misleading:

if (Age == 0)
    yield return new RuleViolation("0 years is not a valid age", "Age");

I know that because you bind to a model property from your Person entity it gets 0 as the default value - why don't you change that in the database then? Set a default in the database to a value that you consider reasonable for the user age, so that it no longer displays as 0 for new entities.

If you don't want to change the db values, use jQuery to set the textbox values to empty strings on your Create page when it loads. If the user tries to submit such a form you can check on the client to disallow it and on the server you don't have to do anything, the user will get a "A value is required" message back.

By the way, you wrote:

i can not compare it with 0 because if someone insert a value 0 then it will be considered as error

I don't see a problem in this particular domain model. Do you think that age of 0 is a good value or not? Ask yourself.

Pawel Krakowiak
+1  A: 

you can change your code with a nullable type?

    Dim i As Nullable(Of Integer)


    MsgBox(i.HasValue)
    'result is false

    i = 1
    MsgBox(i.HasValue)
    'result is true

    i = Nothing
    MsgBox(i.HasValue)
    'result is false
Fredou
A: 

check it with Utilities.GetNullableInt32(textbox1.Text.Trim());

if u check with this getnullableint32 function if the user entered value '0' it will saved as NUll in your table

Aswathaman