欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2016年08月16日 14:59:30   作者:洞庭夕照  
這篇文章主要為大家詳細(xì)介紹了MVC4制作網(wǎng)站教程,用戶注冊功能的實現(xiàn)代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下

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

/// <summary>
 /// 用戶注冊模型
 /// </summary>
 public class UserRegister : User
 {
 /// <summary>
 /// 密碼
 /// </summary>
 [Display(Name="密碼",Description="6-20個字符。")]
 [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>
 /// 驗證碼
 /// </summary>
 [Display(Name = "驗證碼", Description = "請輸入圖片中的驗證碼。")]
 [Required(ErrorMessage = "×")]
 [StringLength(6,MinimumLength=6,ErrorMessage = "×")]
 public string VerificationCode { get; set; }
 }

打開Controllers,在public ActionResult Register()上點右鍵添加視圖,選強類型視圖,模型類選擇UserRegister 

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

@model CMS.Models.UserRegister

@{
 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.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">驗證碼:</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">注冊條款:</div>
  <div class="ctrl">
   @Html.CheckBox("Agreement",new {@class="required"})
   我已閱讀并同意注冊條款
  </div>
  </dd>
  <dd>
  <div class="label"></div>
  <div class="ctrl">
   <input type="submit" value="注冊" />
  </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")
}

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

[HttpPost]
 public ActionResult Register(UserRegister userReg)
 {
  if (Session["VerificationCode"] == null || Session["VerificationCode"].ToString() == "")
  {
  Error _e = new Error { Title = "驗證碼不存在", Details = "在用戶注冊時,服務(wù)器端的驗證碼為空,或向服務(wù)器提交的驗證碼為空", Cause = "<li>你注冊時在注冊頁面停留的時間過久頁已經(jīng)超時</li><li>您繞開客戶端驗證向服務(wù)器提交數(shù)據(jù)</li>", Solution = "返回<a href='" + Url.Action("Register", "User") + "'>注冊</a>頁面,刷新后重新注冊" };
  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", "用戶名已存在");
  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 = "注冊成功", Details = "您已經(jīng)成功注冊,用戶為:" + _user.UserName + " ,請牢記您的密碼!", DwellTime = 5, Navigation = Url.Action("Login", "User") };
  return RedirectToAction("Notice", "Prompt", _n);
  }
  else
  {
  Error _e = new Error { Title = "注冊失敗", Details = "在用戶注冊時,發(fā)生了未知錯誤", Cause = "系統(tǒng)錯誤", Solution = "<li>返回<a href='" + Url.Action("Register", "User") + "'>注冊</a>頁面,輸入正確的信息后重新注冊</li><li>聯(lián)系網(wǎng)站管理員</li>" };
  return RedirectToAction("Error", "Prompt", _e);
  }
 }

OK,運行一下看看效果 

輸入完數(shù)據(jù)點注冊。OK 看到注冊成功的頁面了

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

注冊功能就完成了。

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

相關(guān)文章

  • .NET Core使用FluentEmail發(fā)送郵件的示例代碼

    .NET Core使用FluentEmail發(fā)送郵件的示例代碼

    這篇文章主要介紹了.NET Core使用FluentEmail發(fā)送郵件的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-10-10
  • 用.NET如何生成二維碼

    用.NET如何生成二維碼

    二維碼是用某種特定的幾何圖形按一定規(guī)律在平面(二維方向上)分布的黑白相間的圖形記錄數(shù)據(jù)符號信息的,下面介紹一下如何用.NET生成二維碼(QR Code碼制),需要的朋友可以參考下
    2015-10-10
  • asp.net(C#) 動態(tài)添加非ASP的標(biāo)準(zhǔn)html控件(如添加Script標(biāo)簽)

    asp.net(C#) 動態(tài)添加非ASP的標(biāo)準(zhǔn)html控件(如添加Script標(biāo)簽)

    在開發(fā)程序時,有時需要動態(tài)添加標(biāo)簽,而有部分又不是ASP控件,偶然找到這段代碼,特收藏。
    2009-07-07
  • 詳解ASP.NET MVC下的異步Action的定義和執(zhí)行原理

    詳解ASP.NET MVC下的異步Action的定義和執(zhí)行原理

    這篇文章主要介紹了詳解ASP.NET MVC下的異步Action的定義和執(zhí)行原理,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-12-12
  • .NET連接數(shù)據(jù)庫以及基本的增刪改查操作教程

    .NET連接數(shù)據(jù)庫以及基本的增刪改查操作教程

    這篇文章主要給大家介紹了關(guān)于.NET連接數(shù)據(jù)庫以及基本的增刪改查操作教程的相關(guān)資料,對于剛?cè)腴T的新手們來說是個很好的入門教程,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2018-01-01
  • .net indexOf(String.indexOf 方法)

    .net indexOf(String.indexOf 方法)

    字符串的IndexOf()方法搜索在該字符串上是否出現(xiàn)了作為參數(shù)傳遞的字符串,如果找到字符串,則返回字符的起始位置 (0表示第一個字符,1表示第二個字符依此類推)如果說沒有找到則返回 -1
    2012-10-10
  • 輕量級ORM框架Dapper應(yīng)用之Dapper支持存儲過程

    輕量級ORM框架Dapper應(yīng)用之Dapper支持存儲過程

    這篇文章介紹了Dapper支持使用存儲過程的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-03-03
  • CorFlags.exe檢查.NET程序平臺目標(biāo)(Platform Target)的工具

    CorFlags.exe檢查.NET程序平臺目標(biāo)(Platform Target)的工具

    .NET Framework SDK中的一個工具程序: CorFlags.exe。CorFlags.exe不但可查詢.NET組件的平臺目標(biāo)設(shè)定,甚至能直接修改設(shè)定,省去重新編譯的工夫。
    2013-02-02
  • Visual studio 2017添加引用時報錯未能正確加載ReferenceManagerPackage包的解決方法

    Visual studio 2017添加引用時報錯未能正確加載ReferenceManagerPackage包的解決方法

    這篇文章主要介紹了VS2017添加引用時報錯未能正確加載ReferenceManagerPackage包的解決方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-04-04
  • JWT + ASP.NET MVC時間戳防止重放攻擊詳解

    JWT + ASP.NET MVC時間戳防止重放攻擊詳解

    這篇文章主要給大家介紹了關(guān)于JWT + ASP.NET MVC時間戳防止重放攻擊發(fā)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-07-07

最新評論