try { UpdateModel(post, new[] { "Title", "Content", "Slug" }); } catch { return View(post); } if (string.IsNullOrEmpty(post.Title)) { ViewData.ModelState.AddModelError("Title", post.Title, "标题不能为空"); } if (string.IsNullOrEmpty(post.Content)) { ViewData.ModelState.AddModelError("Content", post.Content, "内容不能为空"); } if (!ViewData.ModelState.IsValid) { return View(post); }
ASP.NET MVC 入门8、ModelState与数据验证,布布扣,bubuko.com
ASP.NET MVC 入门8、ModelState与数据验证
原文:http://www.cnblogs.com/ybtools/p/3796757.html