MVC 校验

@model MVCFirst.Models.Person
@{
    Layout = null;
}


<!DOCTYPE html>


<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Add</title>
    <script src="~/Scripts/jquery-1.10.2.min.js"></script>
    <script src="~/Scripts/jquery.validate.min.js"></script>
    <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
</head>
<body>
    <div> 
        @using (Html.BeginForm("Add", "Person", FormMethod.Post))
        {
            @Html.TextBoxFor(p => p.Id)
            @Html.ValidationMessageFor(p=>p.Id)
            @Html.TextBoxFor(p=>p.Name)
            <input type="submit" name="name" value="Add" />
        }
    </div>
</body>

</html>


using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;


namespace MVCFirst.Models
{
    public class Person
    {
        [Required(ErrorMessage ="编号不能为空")]
        [Range(10,100,ErrorMessage ="必须在10到100之间")]

        public int Id { get; set; }
        public string Name { get; set; }
    }




}

原文地址:https://www.cnblogs.com/dxmfans/p/9434747.html