Models
1 public class Student 2 { 3 public int Id { get; set; } 4 [Required(ErrorMessage = "姓名不能为空!")] 5 public string Name { get; set; } 6 [Range(1, 100, ErrorMessage = "年龄只能在1-100之间!")] 7 public int Age { get; set; } 8 public string Sex { get; set; } 9 [StringLength(10, ErrorMessage = "输入地址过长!")]10 public string Address { get; set; }11 [RegularExpression(@"[a-z,0-9,A-Z,_]+@\w+.((com|cn)|(net.cn|net))", ErrorMessage = "email格式不正确!")]12 public string Email { get; set; }13 14 }
1 public class StudentEntity:DbContext2 {3 public DbSetStudents { get; set; }4 5 }
Controllers
1 public class HomeController : Controller 2 { 3 // 4 // GET: /Home/ 5 6 public ActionResult Index() 7 { 8 return View(); 9 }10 StudentEntity entity = new StudentEntity();11 public ActionResult Add(Student stu)12 {13 if (ModelState.IsValid==true)14 {15 entity.Students.Add(stu);16 entity.SaveChanges();17 }18 return View();19 }20 21 }
View
1 @model Modelverification.Models.Student 2 3 @{ 4 Layout = null; 5 } 6 7 8 9 10 11 12Index 13 14 15 16 17 1819 2930 31