首页 > 扩展 > MVC > 正文

MVC4制作网站教程第二章 用户密码修改2.3

2024-09-06 20:44:36
字体:
来源:转载
供稿:网友

一用户
1.1用户注册 
1.2用户登录 
1.3修改密码 

修改密码会用到一个UserChangePassword 模型类,先添加UserChangePassword类 

/// <summary>  /// 用户修改密码模型  /// </summary>  [NotMapped]  public class UserChangePassword  {    /// <summary>    /// 原密码    /// </summary>    [Display(Name = "原密码")]    [Required(ErrorMessage = "×")]    [StringLength(20, MinimumLength = 6, ErrorMessage = "×")]    [DataType(DataType.Password)]    public string Password { get; set; }    /// <summary>    /// 新密码    /// </summary>    [Display(Name = "新密码", Description = "6-20个字符。")]    [Required(ErrorMessage = "×")]    [StringLength(20, MinimumLength = 6, ErrorMessage = "×")]    [DataType(DataType.Password)]    public string NewPassword { get; set; }    /// <summary>    /// 确认密码    /// </summary>    [Display(Name = "确认密码", Description = "再次输入密码。")]    [Compare("NewPassword", ErrorMessage = "×")]    [DataType(DataType.Password)]    public string ConfirmPassword { get; set; }  }

用到了数据库的更新,先打开UserRepository.cs 修改Update函数

/// <summary>    /// 更新用户信息    /// </summary>    /// <param name="user"></param>    /// <returns></returns>    public override bool Update(User user)    {      dbContext.Users.Attach(user);      dbContext.Entry<User>(user).State = System.Data.EntityState.Modified;      if (dbContext.SaveChanges() > 0) return true;      else return false;    }

在修改密码时需要查出用户信息并更新,需要添加Find(string UserName) 函数 

/// <summary>    /// 查找用户    /// </summary>    /// <param name="UserName">用户名</param>    /// <returns></returns>    public User Find(string UserName)    {      return dbContext.Users.SingleOrDefault(u => u.UserName == UserName);    }

好了打开UserController,添加ChangePassword Action 

/// <summary>    /// 修改密码    /// </summary>    /// <returns></returns>    [UserAuthorize]    public ActionResult ChangePassword()    {      return View();    }    [HttpPost]    [UserAuthorize]    public ActionResult ChangePassword()    {      return View();    }

添加强类型(UserChangePassword)视图,在视图中添加三个文本框,修改后的视图代码 

@model Ninesky.Models.UserChangePassword@{   ViewBag.Title = "修改密码";  Layout = "~/Views/Shared/_Layout.cshtml";}<div class="banner">  <img src="~/Skins/Default/Images/banner.jpg" /></div>@using (Html.BeginForm()){  @Html.ValidationSummary(true)  <div class="form">    <dl>      <dt>修改密码</dt>      <dd>        <div class="label">@Html.LabelFor(model => model.Password):</div>        <div class="ctrl">@Html.PasswordFor(model => model.Password)          @Html.ValidationMessageFor(model => model.Password)          @Html.DisplayDescriptionFor(model => model.Password)        </div>      </dd>      <dd>        <div class="label">@Html.LabelFor(model => model.NewPassword):</div>        <div class="ctrl">@Html.PasswordFor(model => model.NewPassword)          @Html.ValidationMessageFor(model => model.NewPassword)          @Html.DisplayDescriptionFor(model => model.NewPassword)        </div>      </dd>      <dd>        <div class="label">@Html.LabelFor(model => model.ConfirmPassword):</div>        <div class="ctrl">@Html.PasswordFor(model => model.ConfirmPassword)          @Html.ValidationMessageFor(model => model.ConfirmPassword)          @Html.DisplayDescriptionFor(model => model.ConfirmPassword)        </div>      </dd>      <dd>        <div class="label"></div>        <div class="ctrl">          <input type="submit" value="修改密码" />@Html.ValidationMessage("Message")        </div>      </dd>    </dl>    <div class="clear"></div>  </div>}@section Scripts {  @Scripts.Render("~/bundles/jqueryval")}            
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表