Nullable<int> 上的 MVC 4 DataAnnotations

MVC 4 DataAnnotations on Nullable<int>

我正在尝试在 MVC 模型上使用以下内容 class,但在输入字母时没有出现预期的错误。我希望此字段只接受数字。

[RegularExpression("[0-9]*", ErrorMessage = "Must be integer.")]
public Nullable<int> Runtime { get; set; }

令我感到奇怪的是,错误仅在输入字母和数字的组合时显示。 (即 FFF5758)

有人可以帮我理解哪里出了问题吗?

非常感谢!

控制器:

    //
    // GET: /Movies/Create

    public ActionResult Create()
    {
        ViewBag.RatingId = new SelectList(db.Ratings, "RatingId", "Rating");
        return View();
    }

    //
    // POST: /Movies/Create

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create(Movies movies)
    {
        if (ModelState.IsValid)
        {
            db.Movies.Add(movies);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        ViewBag.RatingId = new SelectList(db.Ratings, "RatingId", "Rating", movies.RatingId);
        return View(movies);
    }

查看:

<div class="editor-label">
    @Html.LabelFor(model => model.Runtime)
</div>
<div class="editor-field">
    @Html.EditorFor(model => model.Runtime)
    @Html.ValidationMessageFor(model => model.Runtime)
</div>

试试这个:

    [RegularExpression(@"[0-9]*", ErrorMessage = "Must be integer.")]
    [Range(typeof(int), "30", "300", ErrorMessage = "Must be a sensible integer value.")]
    public int? Runtime { get; set; }

这也意味着运行时间不能超过十亿分钟。