我有一个有点复杂的模型。我有我的UserViewModel,它有几个属性,其中两个是HomePhone和WorkPhone。两者都是PhoneViewModel类型。在PhoneViewModel中,我有CountryCode、AreaCode和Number所有字符串。我想让CountryCode可选,但AreaCode和Number是必需的。这很好用。我的问题是,在UserViewModel中,WorkPhone是强制性的,而HomePhone不是。我是否可以通过在HomeWork属性中设置任何属性来取消PhoneViewModel中的Require属性?我已经试过了:[Valida
我一直在我的razorView中将一个枚举表示为一个隐藏字段,它被回传到一个操作结果中。我注意到,当它绑定(bind)HTML中提供的字符串值时,它会自动验证枚举的值。//////QuizTypesEnum///publicenumQuizType{//////ScoredQuiz///Scored=0,//////PersonalityTypeQuiz///Personality=1}Razor:@Html.HiddenFor(x=>x.QuizType)呈现的HTML:如果我将DOM中的值更改为不正确的值并提交表单,ModelState.IsValid返回false并将以下错误添加
问题我知道有很多方法可以在MVC中进行模型验证,并且有很多关于这个主题的文档。但是,我不太确定验证模型属性的最佳方法是什么,“子模型”属于相同类型。请记住以下几点我仍然想利用TryUpdateModel/TryValidateModel方法这些“子模型”中的每一个都有强类型ViewMainModel类有一个强类型View,用于呈现整体显示View这听起来可能有点令人困惑,但我会投入一些代码来澄清。以下类为例:主模型:classMainModel{publicSomeSubModelProp1{get;set;}publicSomeSubModelProp2{get;set;}}一些子模
我正在尝试使用正则表达式来验证电话号码并在提交无效号码或电话号码时返回错误。MVC代码:PhoneNumber:@Html.TextBoxFor(model=>model.PhoneNumber,new{@class="textbox"})@Html.ValidationMessageFor(model=>model.PhoneNumber)C#代码:[DataType(DataType.PhoneNumber)][Display(Name="PhoneNumber")][Required(ErrorMessage="PhoneNumberRequired!")][RegularExp
我正在尝试使用正则表达式来验证电话号码并在提交无效号码或电话号码时返回错误。MVC代码:PhoneNumber:@Html.TextBoxFor(model=>model.PhoneNumber,new{@class="textbox"})@Html.ValidationMessageFor(model=>model.PhoneNumber)C#代码:[DataType(DataType.PhoneNumber)][Display(Name="PhoneNumber")][Required(ErrorMessage="PhoneNumberRequired!")][RegularExp
我想创建一个自定义验证属性,我想在其中将我的属性值与我的模型类中的另一个属性值进行比较。例如我的模型类中有:...publicstringSourceCity{get;set;}publicstringDestinationCity{get;set;}我想创建一个自定义属性来像这样使用它:[Custom("SourceCity",ErrorMessage="thesourceanddestinationshouldnotbeequal")]publicstringDestinationCity{get;set;}//thiswillcompareSourceCitywithDestin
我想创建一个自定义验证属性,我想在其中将我的属性值与我的模型类中的另一个属性值进行比较。例如我的模型类中有:...publicstringSourceCity{get;set;}publicstringDestinationCity{get;set;}我想创建一个自定义属性来像这样使用它:[Custom("SourceCity",ErrorMessage="thesourceanddestinationshouldnotbeequal")]publicstringDestinationCity{get;set;}//thiswillcompareSourceCitywithDestin