MVC 万无一失的验证 - 无法读取未定义的 属性 'value'
MVC Foolproof validation - Cannot read property 'value' of undefined
我在我的应用程序中使用 MVC Foolproof validation
。
场景是我有一个名为 CustomerType 的下拉列表,具有以下值
Id Name
1 Student
2 Non-Employed
3 Employed
4 SelfEmployed
我的视图模型 public string CompanyAddress{ get; set; }
中还有一个 属性。我的目标是使 CompanyAddress required if
下拉列表具有值 3 or 4
我试过以下
[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"
));
除非您要生成输入(例如)<input name="IsCompanyAddressRequired" value="false" />
(或 value="true"
,具体取决于 customerTypeID
的初始值,否则您的验证属性无法在客户端的客户端上运行) 然后在下拉列表值更改时使用 javascript 动态更新值属性。
改为使用
[RequiredIf("customerTypeID", Operator.GreaterThan, 2, ErrorMessage = "..")]
public string CompanyAddress { get; set; }
并删除你的 IsCompanyAddressRequired
属性.
我在我的应用程序中使用 MVC Foolproof validation
。
场景是我有一个名为 CustomerType 的下拉列表,具有以下值
Id Name
1 Student
2 Non-Employed
3 Employed
4 SelfEmployed
我的视图模型 public string CompanyAddress{ get; set; }
中还有一个 属性。我的目标是使 CompanyAddress required if
下拉列表具有值 3 or 4
我试过以下
[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"
));
除非您要生成输入(例如)<input name="IsCompanyAddressRequired" value="false" />
(或 value="true"
,具体取决于 customerTypeID
的初始值,否则您的验证属性无法在客户端的客户端上运行) 然后在下拉列表值更改时使用 javascript 动态更新值属性。
改为使用
[RequiredIf("customerTypeID", Operator.GreaterThan, 2, ErrorMessage = "..")]
public string CompanyAddress { get; set; }
并删除你的 IsCompanyAddressRequired
属性.