MVC 4范围验证不接受最小值 [英] MVC 4 Range validator not accepting minimum value

查看:125
本文介绍了MVC 4范围验证不接受最小值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用范围验证器在我的asp.net mvc的4项目。在视图模型我说:

I need to use Range Validator in my asp.net mvc 4 project. In view model I added:

[Range(0, int.MaxValue, ErrorMessage = "The Region field is required.")]
public int Region { get; set; }

和控制器中我通过 ModelState.IsValid 检查了。

And in controller i checked it by ModelState.IsValid.

问题是ModelState中不接受 0 作为正常价值 - 这是奇怪,因为我设置范围的 0 最大INT ,所以我认为 0 属于集合(像MVC 5,如果我记得)。我错了,或者它只是一个错误?任何其他值正确读取( 1 不接受和 1001 是确定)。

The problem is ModelState don't accept 0 as a properly value - it's strange because I set range from 0 to max int, so I thought that 0 belong to collection (like in MVC 5 if I remember). Am I wrong or it's just a bug? Any others value is reading properly (-1 is not accepting and 1001 is ok).

更新1

调试1

(就像你在上面的图片中看到我不和我的模型东西之前我没有检查,如果模型是有效的)。

(Like you see in above pictures I don't anything with my model before I not check if model is valid).

注册方式:

[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类:

Full RegisterViewModel class:

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

据我已经使用 ValidateModel()功能@pilotcam建议。在调试模式下我HRESULT: -2146233079 - 我试着将它转换成系统错误code( -2146233079&安培;为0xFFFF = 5385 ),但不是的系统错误codeS列表

According to @pilotcam advice I have used ValidateModel() function. In debug mode I got HResult: -2146233079 - I was trying convert it into System Error Code (-2146233079 & 0xFFFF = 5385) but is not a part of System Error Codes list.

推荐答案

Finnaly我找到了答案。这个问题在我的类型字段(公众诠释地区{获取;设置;} )。如果我没有设置任何东西,MVC试图设置为null,地区不过的 INT 的控制器中调试我仍然得到<$期间不为空的如此C $ C> 0 而不是这是不明确的。

Finnaly I've found answer. The problem was in type of my field (public int Region { get; set; }). If I don't set anything, mvc trying set null to Region but int isn't nullable so in controller during debugging I still getting 0 instead null which was ambiguous.

我没有 [必需] 属性,而不是可空类型自动添加它(错误名称是必需的X字段...是啊,就像在)我的自定义错误消息[范围] 属性。

I didn't have [Required] attribute, but not-nullable type added it automatically (with error name "The X field is required" ... yeah, just like my custom error message in [Range] attribute).

解决的办法是改变:

public int Region { get; set; }

public int? Region { get; set; }

...在视图模型。

... in viewmodel.

这篇关于MVC 4范围验证不接受最小值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆