![](/img/trans.png)
[英]what is the string maximum and minimum value in range validator in asp.net?
[英]MVC 4 Range validator not accepting minimum value
我需要在asp.net mvc 4项目中使用Range Validator。 在视图模型中,我添加了:
[Range(0, int.MaxValue, ErrorMessage = "The Region field is required.")]
public int Region { get; set; }
在控制器中,我通过ModelState.IsValid
检查了它。
问题是ModelState不接受0
作为适当的值-这很奇怪,因为我将范围设置为 0
到max int
,所以我认为0
属于集合(就像我记得在MVC 5中一样)。 我是错了还是只是一个错误? 其他任何值都在正确读取( -1
不接受, 1001
可以)。
更新1
(就像您在上面的图片中看到的那样,在不检查模型是否有效之前,我对模型没有任何要求)。
注册方法:
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Register(RegisterModel model)
{
if (ModelState.IsValid)
{
//I was never here! Always ModelState.IsValid returns false
}
// If we got this far, something failed, redisplay form
ViewBag.Regions = _database.Department.ToList();
return View(model);
}
完整的RegisterViewModel类:
public class RegisterModel
{
[Required]
[Display(Name = "User name")]
public string UserName { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
[Required]
[Display(Name = "Role")]
public int Role { get; set; }
[Range(0, int.MaxValue, ErrorMessage = "The Region field is required.")]
[Display(Name = "Region")]
public int Region { get; set; }
}
注册视图:
<fieldset>
<legend>Registration Form</legend>
<ol>
<li>
@Html.LabelFor(m => m.UserName)
@Html.TextBoxFor(m => m.UserName)
</li>
<li>
@Html.LabelFor(m => m.Password)
@Html.PasswordFor(m => m.Password)
</li>
<li>
@Html.LabelFor(m => m.ConfirmPassword)
@Html.PasswordFor(m => m.ConfirmPassword)
</li>
<li>
@Html.LabelFor(m => m.Role)
<select id="Role" name="Role" data-val-required="The Role field is required." data-val="true">
<option value="">Choose role for user</option>
<option value="1">Administrator</option>
<option value="2">Inspector</option>
</select>
</li>
<li id="region-section">
@Html.LabelFor(m => m.Region)
<select id="Region" name="Region" data-val-required="The Region field is required." data-val="true">
<option value="" selected>Choose region</option>
@foreach (var item in ViewBag.Regions)
{
<option value="@item.Id">@item.Name</option>
}
</select>
</li>
</ol>
<input type="submit" value="Register" />
</fieldset>
更新2
根据@pilotcam的建议,我已经使用ValidateModel()
函数。 在调试模式下,我得到了HResult: -2146233079
我试图将其转换为系统错误代码( -2146233079 & 0xFFFF = 5385
),但它不是系统错误代码列表的一部分。
建议不要使用ViewBag,而是向您的Register类中添加Regions属性,如下所示:
public class RegisterModel
{
// ... existing code
public SelectList Regions { get; set; }
}
这需要对您的操作方法进行一些更新:
// your GET action
public ActionResult Register()
{
var model = new RegisterModel();
model.Regions = new SelectList(_database.Department, "Id", "Name", model.Region);
return View(model);
}
然后,最后更新您的视图:
<li id="region-section">
@Html.LabelFor(m => m.Region)
@Html.DropDownListFor(m => m.Region, Model.Regions, "Select one...")
</li>
根据我的经验,这似乎比使用ViewBag并手动编写<select>
元素更加一致。
很好,我找到了答案。 问题出在我的字段类型上( public int Region { get; set; }
)。 如果我没有设置任何内容,则mvc尝试将null设置为Region
但int不可为空,因此在控制器中,在调试期间,我仍然得到0
而不是null
,这是模棱两可的。
我没有[Required]
属性,但是不是可以为null的类型会自动添加它(错误名称为“ X字段是必填字段” ...是的,就像我在[Range]
属性中自定义错误消息一样)。
解决方案是更改:
public int Region { get; set; }
至:
public int? Region { get; set; }
...在视图模型中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.