用户部分要实现的功能有:
序
一、用户
1.1用户注册
1.2用户登录
1.3修改密码
1.4修改资料
要实现这些目的首先新建用户(Model)模型
/// <summary> /// 用户模型 /// </summary> public class User { [Key] public int UserId { get; set; } /// <summary> /// 用户组Id /// </summary> [Display(Name="用户组Id")] [Required(ErrorMessage = "×")] public int GroupId { get; set; } /// <summary> /// 用户名 /// </summary> [Display(Name="用户名",Description="4-20个字符。")] [Required(ErrorMessage = "×")] [StringLength(20, MinimumLength = 4, ErrorMessage = "×")] public string UserName { get; set; } /// <summary> /// 密码 /// </summary> [Required] [StringLength(256)] public string Password { get; set; } /// <summary> /// 性别【0-男;1-女;2-保密】 /// </summary> [Display(Name="性别")] [Required(ErrorMessage = "×")] [Range(0,2,ErrorMessage = "×")] public byte Gender { get; set; } /// <summary> /// Email /// </summary> [Display(Name="Email",Description="请输入您常用的Email。")] [Required(ErrorMessage = "×")] [EmailAddress(ErrorMessage = "×")] public string Email { get; set; } /// <summary> /// 密保问题 /// </summary> [Display(Name="密保问题",Description="请正确填写,在您忘记密码时用户找回密码。4-20个字符。")] [Required(ErrorMessage = "×")] [StringLength(20, MinimumLength = 4, ErrorMessage = "×")] public string SecurityQuestion { get; set; } /// <summary> /// 密保答案 /// </summary> [Display(Name="密保答案",Description="请认真填写,忘记密码后回答正确才能找回密码。2-20个字符。")] [Required(ErrorMessage = "×")] [StringLength(20, MinimumLength = 2, ErrorMessage = "×")] public string SecurityAnswer { get; set; } /// <summary> /// QQ号码 /// </summary> [Display(Name="QQ号码")] [RegularExpression("^[1-9][0-9]{4-13]$",ErrorMessage = "×")] [StringLength(12, MinimumLength = 6, ErrorMessage = "×")] public string QQ { get; set; } /// <summary> /// 电话号码 /// </summary> [Display(Name="电话号码",Description="常用的联系电话(手机或固话),固话格式为:区号-号码。")] [RegularExpression("^[0-9-]{11-13}$",ErrorMessage = "×")] public string Tel { get; set; } /// <summary> /// 联系地址 /// </summary> [Display(Name="联系地址",Description="常用地址,最多80个字符。")] [StringLength(80, ErrorMessage = "×")] public string Address { get; set; } /// <summary> /// 邮编 /// </summary> [Display(Name="邮编")] [RegularExpression("^[0-9]{6}$",ErrorMessage = "×")] public string PostCode { get; set; } /// <summary> /// 注册时间 /// </summary> public DateTime? RegTime { get; set; } /// <summary> /// 上次登录时间 /// </summary> public DateTime? LastLoginTime { get; set; } /// <summary> /// 用户组 /// </summary> public virtual UserGroup Group { get; set; } }
下面添加用户Controller。在Controller文件夹点右键 选择新建控制器名称输入UserController,完成后生成代码如下:
using System.Web.Mvc; using CMS.Models; namespace CMS.Controllers { public class UserController : Controller { public ActionResult Register() { return View(); } // // POST: /User/Create [HttpPost] public ActionResult Register(UserRegister uerReg) { try { // TODO: Add insert logic here return RedirectToAction("Index"); } catch { return View(); } } } }
马上涉及的问题就是数据的存取,本来准备用Repository模式做,后来觉得用Repository模式在一个人写个简单那网站的情况下意义不大,写起来反而有点啰嗦,干脆用伪Repository模式写。项目中新建Repository文件夹。在文件夹中新建类CMS,用来实现EF的Context。代码如下:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data.Entity; using CMS.Models; namespace CMS.Repository { public class CMSContext:DbContext { public DbSet<User> Users { get; set; } } }
简单吧。再建一个类RepositoryBase,用来做***Repository的基类,里面写Add,Update,Delete,Find几个虚函数,方便在继承类里面这几个函数的名称统一。
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace CMS.Repository { public class RepositoryBase<TModel> { private CMSContext dbContext; public RepositoryBase() { dbContext = new CMSContext(); } /// <summary> /// 添加【继承类重写后才能正常使用】 /// </summary> public virtual bool Add(TModel Tmodel) { return false; } /// <summary> /// 更新【继承类重写后才能正常使用】 /// </summary> public virtual bool Update(TModel Tmodel) { return false; } /// <summary> /// 删除【继承类重写后才能正常使用】 /// </summary> public virtual bool Delete(int Id) { return false; } /// <summary> /// 查找指定值【继承类重写后才能正常使用】 /// </summary> public virtual TModel Find(int Id) { return default(TModel); } ~RepositoryBase() { if (dbContext != null) { dbContext.Dispose(); } } } }
可以写UserRepository的具体实现了,新建UserRepository类。重写Add,Update方法后代码如下: