你的位置:首页 > ASP.net教程

[ASP.net教程]mvc+三层 网站项目:用户注册


第一:前文,和登录一个控制器下

        public ActionResult register(string UserName, string PassWord, string ConfirmPassword)
        {
              

            if (!string.IsNullOrEmpty(UserName))//用户名不等于空
            {
                string sql = "SELECT COUNT(1) FROM Manager WHERE UserName='" + UserName + "'";//查询
                var result = new MySqlAdoProvide().ExecuteScalar<int>(MySqlHelper.connectionStringManager, sql, null);

                if (result > 0)//用户名大于0
                {
                    ModelState.AddModelError("UserName", "用户名存在,请重新输入!");
                }
                else
                {
                    if (PassWord.ToLower() != ConfirmPassword.ToLower())//两次密码输入不一致
                    {
                        ModelState.AddModelError("Messge", "密码不一致,请重新输入!");
                    }

                    string InserStr = "Insert into Manager(userName,userPassWord,RoleId,IsEnable,CreateTime,Remark,IsDel,CreateUserId,UpdateUserId,DelUserId)values('" + UserName + "','" + PassWord + "',0,default,default,0,0,0,0,0)";//插入

                    var results = new MySqlAdoProvide().ExecuteNonQuery(MySqlHelper.connectionStringManager, InserStr, null) > 0;
                    if (results)
                    {
                        ModelState.AddModelError("Messge", "注册成功111");
                    }
                    else
                    {
                        ModelState.AddModelError("Messge", "注册失败!");
                    }
 
                }
            }

          
            return View();
        }

第二:注册界面视图

   @using (Html.BeginForm("register", null, FormMethod.Post, new { @class = "m-t" }))//和<form>一样,只不过这个更好,因为如果修改视图地址,<from>要修改很多,这个不需要
            {
                <div >
                    @Html.TextBoxFor(m => m.UserName, new { @class = "form-control", @placeholder = "请输入用户名" })
                    @Html.ValidationMessageFor(m => m.UserName)
                </div>
                <div >
                    @Html.TextBoxFor(m => m.Password, new { @class = "form-control", @placeholder = "请输入密码", @type = "password" })
                    @Html.ValidationMessageFor(m => m.Password)
                </div>
                <div >
                    @Html.TextBoxFor(m => m.ConfirmPassword, new { @class = "form-control", @placeholder = "请再次输入密码", @type = "password" })
                    @Html.ValidationMessageFor(m => m.ConfirmPassword)
                </div>
                <div >
                    <div >
                        <label >
                            <input type="checkbox"><i></i> 我同意注册协议
                        </label>
                    </div>
                </div>
                 @Html.ValidationMessageFor(m => m.Messge)
                <button type="submit" >注 册</button>

                <p >
                    <small>已经有账户了?</small><a href="@Url.Action("login")">点此登录</a>
                </p>
            }