Asp.net mvc 4 MVC万无一失的验证-无法读取属性';价值';未定义的
我正在应用程序中使用Asp.net mvc 4 MVC万无一失的验证-无法读取属性';价值';未定义的,asp.net-mvc-4,unobtrusive-validation,foolproof-validation,Asp.net Mvc 4,Unobtrusive Validation,Foolproof Validation,我正在应用程序中使用MVC万无一失的验证。 场景是我有一个名为CustomerType的dropdownlist,它具有以下值 Id Name 1 Student 2 Non-Employed 3 Employed 4 SelfEmployed 我的viewmodel公共字符串CompanyAddress{get;set;}中还有一个属性。我的目标是,如果dropdownlist的值为3或4,则需要CompanyAddress 我试过以
MVC万无一失的验证。
场景是我有一个名为CustomerType的dropdownlist,它具有以下值
Id Name
1 Student
2 Non-Employed
3 Employed
4 SelfEmployed
我的viewmodel公共字符串CompanyAddress{get;set;}
中还有一个属性。我的目标是,如果
dropdownlist的值为3或4,则需要CompanyAddress
我试过以下方法
[Required(ErrorMessage = "Please select status of the customer", AllowEmptyStrings = false)]
public int? customerTypeID { get; set; }
public SelectList customerTypeList { get; set; }
[RequiredIf("IsCompanyAddressRequired", true, ErrorMessage = "please enter company address")]
public string CompanyAddress { get; set; }
public bool IsCompanyAddressRequired
{
get
{
if (customerTypeID == 3 || customerTypeID == 4)
{
return true;
}
else
{
return false;
}
}
}
上面的代码在服务器端正常工作,但在客户端我得到以下错误
`Uncaught TypeError: Cannot read property 'value' of undefined`
下面引用了验证
bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
"~/plugins/jQueryVal/jquery.unobtrusive*",
"~/plugins/jQueryVal/jquery.validate*",
"~/plugins/foolproofValidation/MvcFoolproofJQueryValidation.min.js",
"~/plugins/foolproofValidation/mvcfoolproof.unobtrusive.min.js",
"~/plugins/foolproofValidation/MvcFoolproofValidation.min.js"
));
您的验证属性无法在客户端上的客户端上工作,除非您生成输入(例如)
(或value=“true”
,具体取决于customerTypeID
)的初始值,然后在dropdownlist值更改时使用javascript动态更新value属性
改用
[RequiredIf("customerTypeID", Operator.GreaterThan, 2, ErrorMessage = "..")]
public string CompanyAddress { get; set; }
并删除您的IsCompanyAddressRequired
属性。您的验证属性无法在客户端上的客户端上工作,除非您生成输入(例如)
(或value=“true”
,具体取决于customerTypeID
的初始值)然后在dropdownlist值更改时使用javascript动态更新value属性
改用
[RequiredIf("customerTypeID", Operator.GreaterThan, 2, ErrorMessage = "..")]
public string CompanyAddress { get; set; }
并删除您的IsCompanyAddressRequired
属性。除非您在视图中为属性IsCompanyAddressRequired
生成输入(然后您需要根据dropdownlist中的选定值动态更新该属性)。在读取时,请使用[GreaterThan(“customerTypeID”,2)]
并删除回复的bool
propertyThanks。.这里我得到了一个错误万无一失。GreaterThanAttribute
不包含一个包含两个参数的构造函数。很抱歉,与我自己的验证属性混淆:)-让我检查文档(甚至不确定万无一失是否支持这一点)看起来它需要是[RequiredIf(“customerTypeID”,Operator.GreaterThan,2,ErrorMessage=“…”)]
谢谢配合它的工作,请发布答案:)除非您在视图中为属性IsCompanyAddressRequired
生成输入,否则无法工作(然后需要根据dropdownlist中的选定值动态更新它。在读取时,使用[GreaterThan(“customerTypeID”,2)]
并删除回复的bool
propertyThanks。.这里我得到了一个错误万无一失。GreaterThanAttribute
不包含一个包含两个参数的构造函数。很抱歉,与我自己的验证属性混淆:)-让我检查文档(甚至不确定万无一失是否支持这一点)看起来它需要是[RequiredIf(“customerTypeID”,Operator.GreaterThan,2,ErrorMessage=“…”)]
谢谢配合它的工作,请发布答案:)