在上一篇的EF之DB First中,存在以下的两个问题:
1. 添加/编辑页面显示的是属性名称,而非自定义的名称(如:姓名、专业...)
2. 添加/编辑时没有加入验证
另外数据展示使用分页
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" }) 是显示属性Name的“标签”,如果没有指定Display特性,则直接显示属性名Name
通过数据库生成的实体模型文件与代码一般不直接修改(防止下次生成时覆盖),这里要使用验证与实体分离
添加一个验证类,代码如下 :
using System.ComponentModel.DataAnnotations; namespace Zhong.Web.Models { [MetadataType(typeof(T_StudentValidateInfo))] public partial class T_Student { } public class T_StudentValidateInfo { [Display(Name="姓名")] [Required(ErrorMessage ="姓名不能为空")] [StringLength(10,ErrorMessage ="姓名长度超出限制")] public string Name { get; set; } [Display(Name="学号")] [Required] [StringLength(20,MinimumLength =10,ErrorMessage ="长度为10-20")] public string StudentId { get; set; } } }