English 中文(简体)
避免在MVC3特区进行验证
原标题:Avoid validation on int field MVC3

我有以下看法:

public class Contract{

public string Name {get; set;}

public ContractTerm PrpTerm { get; set; }

}

PRP 术语模式类似

public class PrpTerm{

public int Id {get; set;}
public string Name {get; set;}

}

对我的模型验证 I m 采用XML文档以列入说明。 I ve set the Name property as required and I ve Let ContractTerm without specified any accreditation (it s an optional field on a list). 我的看法是,合同术语上有一个文字箱和一个选择清单。

当Im试图储蓄时,我拿到<条码>ModelState.IsValid财产作假,说<条码>PRPTerm.Id。 需要价值。 我的理解是,这并不是因为我们不能把一刀切的价值视为无效。

有些人是否知道有办法避免这种情况得到验证?

我先尝试使用本德(不包括通知),但从数据提交中删除了外地,这让我获得选定用户的价值(如果情况发生)。 我不能把这种id子 null倒在地图册上。 另一种选择是把《规则一》放在“观点”上,但我需要以合理的理由证明这一改变是合理的。

任何想法?

最佳回答

隐含的<条码>[要求]属性”适用于<条码>。 Id as it is an int which is, by definition, not-nullable (as against int? which is unable.

这种行为在MVC使用

DataAnnotations ModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes

事实并非如此。

You can disable this behaviour by setting it to false in your Global.asax.cs

然而,正如AFinkelstein所指出的,它同真正的问题一样,与你的看法是一样的。 我要建议大家重新思考,而不是放弃对多国军的这种手法。

我建议

public class Contract
{
    public string Name { get; set; }
    public int? PrpTermId { get; set; }
}

Is probably what you actually want. In terms of a valid reason to do this - it is generally accepted that your ViewModels will often differ from your Models, as the purpose of a given ViewModel will likely be slightly different to that of the Models it is made up of.

问题回答

鉴于你不再使用其财产,因此与PrpTerm类一样,对你的《观点模式》来说是适当的。 你们真心想要的是,从下一份名单中删除了PrpTermName。 我会有一个像一类的结构。

public class Contract
{
    public string Name { get; set; }
    public string PrpTermName { get; set; }
    public SelectList ContractTermList { get; set; }
}

现在,你不需要担心身份证的验证,因为它不属于你的观察模式。





相关问题
Bind Button.IsEnabled to custom validation with XAML?

I am sorry I didn t know how to title my question any better, you name it if you got a good 1. I have an entity Contact. this person has navigation properties: Address, Phones (A collection of Phone)....

WPF - MVVM - NHibernate Validation

Im facing a bit of an issue when trying to validate a decimal property on domain object which is bound to a textbox on the view through the viewmodel. I am using NHibernate to decorate my property on ...

Wpf Combobox Limit to List

We are using Wpf Combobox to allow the user to do the following things: 1) select items by typing in the first few characters 2) auto complete the entry by filtering the list 3) suggesting the first ...

Rails 101 | validates_currency?

I ve searched high and low, but I could not find a solution, to what I think seems like a very common task. In a form I want to have a text input that accepts currency strings (i.e. $1,000,000 or ...

CodeIgniter form verification and class

I m using the form validation library and have something like this in the view <p> <label for="NAME">Name <span class="required">*</span></label> <?...

热门标签