主頁(yè) > 知識(shí)庫(kù) > MVC4制作網(wǎng)站教程第二章 用戶(hù)注冊(cè)2.1

MVC4制作網(wǎng)站教程第二章 用戶(hù)注冊(cè)2.1

熱門(mén)標(biāo)簽:電話機(jī)器人鑰匙扣 電銷(xiāo)外呼系統(tǒng)是違法的嗎 電銷(xiāo)專(zhuān)用外呼線路 漯河外呼調(diào)研線路 旅游地圖標(biāo)注線路 廣西房產(chǎn)智能外呼系統(tǒng)推薦 地圖標(biāo)注位置怎么弄圖 400電話唐山辦理 威力最大的電銷(xiāo)機(jī)器人

終于開(kāi)始做用戶(hù)部分了,先做注冊(cè)
一用戶(hù) 
1.1用戶(hù)注冊(cè) 
首先在Models里添加用戶(hù)注冊(cè)模型類(lèi)UserRegister 繼承自User,在類(lèi)中new一下密碼字段,并添加重復(fù)密碼和驗(yàn)證碼字段。完成后的代碼 

/// summary>
 /// 用戶(hù)注冊(cè)模型
 /// /summary>
 public class UserRegister : User
 {
 /// summary>
 /// 密碼
 /// /summary>
 [Display(Name="密碼",Description="6-20個(gè)字符。")]
 [Required(ErrorMessage = "×")]
 [StringLength(20,MinimumLength=6,ErrorMessage = "×")]
 [DataType(DataType.Password)]
 public new string Password { get; set; }
 /// summary>
 /// 確認(rèn)密碼
 /// /summary>
 [Display(Name = "確認(rèn)密碼", Description = "再次輸入密碼。")]
 [Compare("Password", ErrorMessage = "×")]
 [DataType(DataType.Password)]
 public string RePassword { get; set; }
 /// summary>
 /// 驗(yàn)證碼
 /// /summary>
 [Display(Name = "驗(yàn)證碼", Description = "請(qǐng)輸入圖片中的驗(yàn)證碼。")]
 [Required(ErrorMessage = "×")]
 [StringLength(6,MinimumLength=6,ErrorMessage = "×")]
 public string VerificationCode { get; set; }
 }

打開(kāi)Controllers,在public ActionResult Register()上點(diǎn)右鍵添加視圖,選強(qiáng)類(lèi)型視圖,模型類(lèi)選擇UserRegister 

添加完成后轉(zhuǎn)到Register.cshtml編輯視圖,刪除掉自動(dòng)生成的內(nèi)容,手動(dòng)輸入想要的代碼,完成后代碼如下: 

@model CMS.Models.UserRegister

@{
 ViewBag.Title = "用戶(hù)注冊(cè)";
 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>用戶(hù)注冊(cè)/dt>
  dd>
  div class="label">@Html.LabelFor(model => model.UserName):/div>
  div class="ctrl">@Html.EditorFor(model => model.UserName)
   @Html.ValidationMessageFor(model => model.UserName)
   @Html.DisplayDescriptionFor(model => model.UserName)
  /div>
  /dd>
  dd>
  div class="label">@Html.LabelFor(model => model.Gender):/div>
  div class="ctrl">@Html.RadioButton("Gender", 0) 男 @Html.RadioButton("Gender", 1) 女 @Html.RadioButton("Gender", 2, true) 保密
   @Html.ValidationMessageFor(model => model)
   @Html.DisplayDescriptionFor(model => model)
  /div>
  /dd>
  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.RePassword):/div>
  div class="ctrl">@Html.PasswordFor(model => model.RePassword)
   @Html.ValidationMessageFor(model => model.RePassword)
   @Html.DisplayDescriptionFor(model => model.RePassword)
  /div>
  /dd>
  dd>
  div class="label">@Html.LabelFor(model => model.SecurityQuestion):/div>
  div class="ctrl">@Html.EditorFor(model => model.SecurityQuestion)
   @Html.ValidationMessageFor(model => model.SecurityQuestion)
   @Html.DisplayDescriptionFor(model => model.SecurityQuestion)
  /div>
  /dd>
  dd>
  div class="label">@Html.LabelFor(model => model.SecurityAnswer):/div>
  div class="ctrl">@Html.EditorFor(model => model.SecurityAnswer)
   @Html.ValidationMessageFor(model => model.SecurityAnswer)
   @Html.DisplayDescriptionFor(model => model.SecurityAnswer)
  /div>
  /dd>
  dd>
  div class="label">@Html.LabelFor(model => model.Email):/div>
  div class="ctrl">@Html.EditorFor(model => model.Email)
   @Html.ValidationMessageFor(model => model.Email)
   @Html.DisplayDescriptionFor(model => model.Email)
  /div>
  /dd>
  dd>
  div class="label">驗(yàn)證碼:/div>
  div class="ctrl">
   @Html.TextBoxFor(model => model.VerificationCode)
   @Html.ValidationMessageFor(model => model.VerificationCode)
   img id="verificationcode" alt="" src="@Url.Action("VerificationCode", "User")" />
   a id="trydifferent" style="cursor:pointer">換一張/a>
  /div>
  /dd>
  dd>
  div class="label">注冊(cè)條款:/div>
  div class="ctrl">
   @Html.CheckBox("Agreement",new {@class="required"})
   我已閱讀并同意注冊(cè)條款
  /div>
  /dd>
  dd>
  div class="label">/div>
  div class="ctrl">
   input type="submit" value="注冊(cè)" />
  /div>
  /dd>
 /dl>
 div class="clear">/div>
 /div>
}
script type="text/javascript" >
 $("#trydifferent").click(function () {
 $("#verificationcode").attr("src", "/User/VerificationCode?"+new Date());
 })
 
/script>
@section Scripts {
 @Scripts.Render("~/bundles/jqueryval")
}

下面開(kāi)始寫(xiě)注冊(cè)處理的代碼。 
在Controllers,在public ActionResult Register(){return View();}下面添加一個(gè)[HttpPost]方式的Register() Action,代碼如下: 

[HttpPost]
 public ActionResult Register(UserRegister userReg)
 {
  if (Session["VerificationCode"] == null || Session["VerificationCode"].ToString() == "")
  {
  Error _e = new Error { Title = "驗(yàn)證碼不存在", Details = "在用戶(hù)注冊(cè)時(shí),服務(wù)器端的驗(yàn)證碼為空,或向服務(wù)器提交的驗(yàn)證碼為空", Cause = "li>你注冊(cè)時(shí)在注冊(cè)頁(yè)面停留的時(shí)間過(guò)久頁(yè)已經(jīng)超時(shí)/li>li>您繞開(kāi)客戶(hù)端驗(yàn)證向服務(wù)器提交數(shù)據(jù)/li>", Solution = "返回a href='" + Url.Action("Register", "User") + "'>注冊(cè)/a>頁(yè)面,刷新后重新注冊(cè)" };
  return RedirectToAction("Error", "Prompt", _e);
  }
  else if (Session["VerificationCode"].ToString() != userReg.VerificationCode.ToUpper())
  {
  ModelState.AddModelError("VerificationCode", "×");
  return View();
  }
  userRsy = new UserRepository();
  if (userRsy.Exists(userReg.UserName))
  {
  ModelState.AddModelError("UserName", "用戶(hù)名已存在");
  return View();
  }
  User _user = userReg;
  _user.Password = Common.Text.Sha256(userReg.Password);
  _user.RegTime = System.DateTime.Now;
  if (userRsy.Add(_user))
  {
  Notice _n = new Notice { Title = "注冊(cè)成功", Details = "您已經(jīng)成功注冊(cè),用戶(hù)為:" + _user.UserName + " ,請(qǐng)牢記您的密碼!", DwellTime = 5, Navigation = Url.Action("Login", "User") };
  return RedirectToAction("Notice", "Prompt", _n);
  }
  else
  {
  Error _e = new Error { Title = "注冊(cè)失敗", Details = "在用戶(hù)注冊(cè)時(shí),發(fā)生了未知錯(cuò)誤", Cause = "系統(tǒng)錯(cuò)誤", Solution = "li>返回a href='" + Url.Action("Register", "User") + "'>注冊(cè)/a>頁(yè)面,輸入正確的信息后重新注冊(cè)/li>li>聯(lián)系網(wǎng)站管理員/li>" };
  return RedirectToAction("Error", "Prompt", _e);
  }
 }

OK,運(yùn)行一下看看效果 

輸入完數(shù)據(jù)點(diǎn)注冊(cè)。OK 看到注冊(cè)成功的頁(yè)面了

看一下數(shù)據(jù)庫(kù)中也有相應(yīng)記錄了

注冊(cè)功能就完成了。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • ASP.NET MVC5網(wǎng)站開(kāi)發(fā)用戶(hù)登錄、注銷(xiāo)(五)
  • ASP.NET MVC5 網(wǎng)站開(kāi)發(fā)框架模型、數(shù)據(jù)存儲(chǔ)、業(yè)務(wù)邏輯(三)
  • ASP.NET MVC5網(wǎng)站開(kāi)發(fā)概述(一)
  • ASP.NET MVC5網(wǎng)站開(kāi)發(fā)修改及刪除文章(十)
  • ASP.NET MVC5網(wǎng)站開(kāi)發(fā)管理列表、回復(fù)及刪除(十三)
  • ASP.NET MVC5網(wǎng)站開(kāi)發(fā)文章管理架構(gòu)(七)
  • ASP.NET MVC5網(wǎng)站開(kāi)發(fā)咨詢(xún)管理的架構(gòu)(十一)
  • ASP.NET MVC5網(wǎng)站開(kāi)發(fā)之展示層架構(gòu)(五)
  • ASP.NET MVC5網(wǎng)站開(kāi)發(fā)之網(wǎng)站設(shè)置(九)
  • ASP.NET MVC5網(wǎng)站開(kāi)發(fā)之用戶(hù)添加和瀏覽2(七)

標(biāo)簽:湖北 試駕邀約 焦作 銅陵 欽州 綏化 湘西 無(wú)錫

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《MVC4制作網(wǎng)站教程第二章 用戶(hù)注冊(cè)2.1》,本文關(guān)鍵詞  MVC4,制作,網(wǎng)站,教程,第二章,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《MVC4制作網(wǎng)站教程第二章 用戶(hù)注冊(cè)2.1》相關(guān)的同類(lèi)信息!
  • 本頁(yè)收集關(guān)于MVC4制作網(wǎng)站教程第二章 用戶(hù)注冊(cè)2.1的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章