AspNet MVC Entity Framework在Model层 数据验证 validation data

Model:

    [MetadataType(typeof(UserMetaData))]
    public partial class User
    {
    }
    public class UserMetaData
    {
        [Required]
        [StringLength(10)]
        [RegularExpression(@"\w+")]
        public object Username { get; set; }

        [Required]
        public object Password { get; set; }
        [Range(1,10)]
        public object StuNum { get; set; }

        [Required]
        public object Birthday { get; set; }
    }

Controller中:

//
        // POST: /User/Create

        [HttpPost]
        public ActionResult Create( [Bind(Exclude="ID")] TestMvcAppAndEntityFramework.Models.User model)
        {
            try
            {
                // TODO: Add insert logic here
                if ( ModelState.IsValid)
                {
                    return Content("成功");
                    
                }
                else
                {
                    return View();
                }
            }
            catch
            {
                return View();
            }
        }

 

posted @ 2010-12-26 17:23  庚武  Views(529)  Comments(0Edit  收藏  举报