I have integer type field in database which is having property "Not Null".
我在数据库中有整数类型字段,其属性为“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!
当我创建一个视图并进行验证时,如果我将该字段留空,则会将其视为0,因此我无法将其与0进行比较,因为如果有人插入值0,那么它将被视为错误!
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
另一个问题是我正在使用模型错误,如“ASP.NET MVC 1.0”@ Scott Gu博客中所述。我正在检查部分对象类(由LINQ-To-SQL创建)中的值。即
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".
就像在数据库中一样,如果我声明为smallint,即c#中的short,现在如果我超过该范围,那么它会给出错误,因为“需要一个值”。
so finally is there any best way for validation in ASP.NET MVC?
那么最后是否有最好的ASP.NET MVC验证方法?
4 个解决方案
#1
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
#2
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.
我不是很了解MVC的细节,但是有没有办法将age的默认值更改为-1?通过这种方式,您可以查看值,人们仍然可以输入0。
Another solution could be to use a nullable int. You can do this by appending a ? to the type.
另一种解决方案可能是使用可空的int。你可以通过添加一个?到类型。
HTH.
Jonathan
#3
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:
我会先在客户端验证表单。如果年龄为0,则让用户知道它不是有效年龄。在服务器上更改消息,这是误导性的:
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.
我知道,因为你绑定到Person实体的一个模型属性,它得到0作为默认值 - 为什么你不在数据库中更改它?将数据库中的默认值设置为您认为对用户年龄合理的值,以便新实体不再显示为0。
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.
如果您不想更改db值,请在加载时使用jQuery将文本框值设置为Create页面上的空字符串。如果用户尝试提交此类表单,您可以在客户端上检查以禁止它,并且在服务器上您不必执行任何操作,用户将收到“需要A值”消息。
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
我无法将其与0进行比较,因为如果有人插入值0,那么它将被视为错误
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.
我没有在这个特定的域模型中看到问题。你认为0岁是不是一个好的价值?问你自己。
#4
check it with Utilities.GetNullableInt32(textbox1.Text.Trim());
用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
如果你用这个GetNullableInt32()函数检查,如果用户输入值'0',它将在你的表中保存为NUll
#1
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
#2
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.
我不是很了解MVC的细节,但是有没有办法将age的默认值更改为-1?通过这种方式,您可以查看值,人们仍然可以输入0。
Another solution could be to use a nullable int. You can do this by appending a ? to the type.
另一种解决方案可能是使用可空的int。你可以通过添加一个?到类型。
HTH.
Jonathan
#3
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:
我会先在客户端验证表单。如果年龄为0,则让用户知道它不是有效年龄。在服务器上更改消息,这是误导性的:
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.
我知道,因为你绑定到Person实体的一个模型属性,它得到0作为默认值 - 为什么你不在数据库中更改它?将数据库中的默认值设置为您认为对用户年龄合理的值,以便新实体不再显示为0。
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.
如果您不想更改db值,请在加载时使用jQuery将文本框值设置为Create页面上的空字符串。如果用户尝试提交此类表单,您可以在客户端上检查以禁止它,并且在服务器上您不必执行任何操作,用户将收到“需要A值”消息。
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
我无法将其与0进行比较,因为如果有人插入值0,那么它将被视为错误
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.
我没有在这个特定的域模型中看到问题。你认为0岁是不是一个好的价值?问你自己。
#4
check it with Utilities.GetNullableInt32(textbox1.Text.Trim());
用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
如果你用这个GetNullableInt32()函数检查,如果用户输入值'0',它将在你的表中保存为NUll