如何验证整数高度,但是当我将字符串发送错误时,无法将整数转换为字符串 [英] How Do I Do Validation For Height Which Is In Integer But When I Pass String Its Giving Error In Rest Client That Can Not Convert Integer Into String
本文介绍了如何验证整数高度,但是当我将字符串发送错误时,无法将整数转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
[必需(ErrorMessage =输入高度)]
[范围(0,999,ErrorMessage =身高太长)]
[RegularExpression(@ ^ [0-9] + $,ErrorMessage =仅输入数字)]
public Nullable < int > 高度{get;组; }
i我在休息客户中张贴
{
FighterID: 11 ,
身高: 45 ,
重量: 200 ,
到达: 33 ,
}
代替45,当我传递任何字符串时
< pre lang = cs > {
FighterID:11,
身高:ghgh
重量:200,
到达:33,
}
它没有检查验证并给出错误
{
消息 < span class =code-keyword>: 请求无效。
ModelState:{
tblFighters.Height:[2]
0:无法将字符串转换为整数:d。路径'高度',第8行,第12位。
1:输入高度
-
} -
解决方案
,ErrorMessage =仅输入数字)]
public Nullable < int > 高度{get; set;}
i我在休息客户中张贴
{
FighterID: 11 ,
身高: 45 ,
体重:< span class =code-digit> 200 ,
到达: 33 ,
}
代替45,当我传递任何字符串时
< pre lang = cs > {
FighterID:11,
身高:ghgh
重量:200,
到达:33,
}
它没有检查验证并给出错误
{
消息 : 请求无效。
ModelState:{
tblFighters.Height:[2]
0 :无法将字符串转换为整数:d。路径'高度',第8行,第12位。
1:输入高度
-
} -
[Required(ErrorMessage = "Enter Height")]
[Range(0,999, ErrorMessage = "Height is too long")]
[RegularExpression(@"^[0-9]+$", ErrorMessage = "Enter Only Numbers")]
public Nullable<int> Height { get; set; }
i am posting in rest client
{
FighterID: 11,
Height:45,
Weight: 200,
Reach: 33,
}
in place of 45 when i pass any string like
<pre lang="cs">{
FighterID: 11,
Height:"ghgh"
Weight: 200,
Reach: 33,
}
its not checcking validation and giving error
{
Message: "The request is invalid."
ModelState: {
tblFighters.Height: [2]
0: "Could not convert string to integer: d. Path 'Height', line 8, position 12."
1: "Enter Height"
-
}-
解决方案
", ErrorMessage = "Enter Only Numbers")] public Nullable<int> Height { get; set; }
i am posting in rest client
{ FighterID: 11, Height:45, Weight: 200, Reach: 33, }
in place of 45 when i pass any string like
<pre lang="cs">{ FighterID: 11, Height:"ghgh" Weight: 200, Reach: 33, }
its not checcking validation and giving error
{ Message: "The request is invalid." ModelState: { tblFighters.Height: [2] 0: "Could not convert string to integer: d. Path 'Height', line 8, position 12." 1: "Enter Height" - }-
这篇关于如何验证整数高度,但是当我将字符串发送错误时,无法将整数转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文