MVC4制作網(wǎng)站教程第二章 用戶注冊2.1
終于開始做用戶部分了,先做注冊
一用戶
1.1用戶注冊
首先在Models里添加用戶注冊模型類UserRegister 繼承自User,在類中new一下密碼字段,并添加重復(fù)密碼和驗(yàn)證碼字段。完成后的代碼
/// <summary> /// 用戶注冊模型 /// </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 = "請輸入圖片中的驗(yàn)證碼。")] [Required(ErrorMessage = "×")] [StringLength(6,MinimumLength=6,ErrorMessage = "×")] public string VerificationCode { get; set; } }
打開Controllers,在public ActionResult Register()上點(diǎn)右鍵添加視圖,選強(qiáng)類型視圖,模型類選擇UserRegister
添加完成后轉(zhuǎn)到Register.cshtml編輯視圖,刪除掉自動(dòng)生成的內(nèi)容,手動(dòng)輸入想要的代碼,完成后代碼如下:
@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">驗(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">注冊條款:</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();}下面添加一個(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 = "在用戶注冊時(shí),服務(wù)器端的驗(yàn)證碼為空,或向服務(wù)器提交的驗(yàn)證碼為空", Cause = "<li>你注冊時(shí)在注冊頁面停留的時(shí)間過久頁已經(jīng)超時(shí)</li><li>您繞開客戶端驗(yàn)證向服務(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 = "在用戶注冊時(shí),發(fā)生了未知錯(cuò)誤", Cause = "系統(tǒng)錯(cuò)誤", Solution = "<li>返回<a href='" + Url.Action("Register", "User") + "'>注冊</a>頁面,輸入正確的信息后重新注冊</li><li>聯(lián)系網(wǎng)站管理員</li>" }; return RedirectToAction("Error", "Prompt", _e); } }
OK,運(yùn)行一下看看效果
輸入完數(shù)據(jù)點(diǎn)注冊。OK 看到注冊成功的頁面了
看一下數(shù)據(jù)庫中也有相應(yīng)記錄了
注冊功能就完成了。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- ASP.NET?MVC5網(wǎng)站開發(fā)用戶登錄、注銷(五)
- ASP.NET?MVC5?網(wǎng)站開發(fā)框架模型、數(shù)據(jù)存儲(chǔ)、業(yè)務(wù)邏輯(三)
- ASP.NET MVC5網(wǎng)站開發(fā)概述(一)
- ASP.NET MVC5網(wǎng)站開發(fā)修改及刪除文章(十)
- ASP.NET MVC5網(wǎng)站開發(fā)管理列表、回復(fù)及刪除(十三)
- ASP.NET MVC5網(wǎng)站開發(fā)文章管理架構(gòu)(七)
- ASP.NET?MVC5網(wǎng)站開發(fā)咨詢管理的架構(gòu)(十一)
- ASP.NET MVC5網(wǎng)站開發(fā)之展示層架構(gòu)(五)
- ASP.NET?MVC5網(wǎng)站開發(fā)之網(wǎng)站設(shè)置(九)
- ASP.NET?MVC5網(wǎng)站開發(fā)之用戶添加和瀏覽2(七)
相關(guān)文章
.NET Core使用FluentEmail發(fā)送郵件的示例代碼
這篇文章主要介紹了.NET Core使用FluentEmail發(fā)送郵件的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10asp.net(C#) 動(dòng)態(tài)添加非ASP的標(biāo)準(zhǔn)html控件(如添加Script標(biāo)簽)
在開發(fā)程序時(shí),有時(shí)需要?jiǎng)討B(tài)添加標(biāo)簽,而有部分又不是ASP控件,偶然找到這段代碼,特收藏。2009-07-07詳解ASP.NET MVC下的異步Action的定義和執(zhí)行原理
這篇文章主要介紹了詳解ASP.NET MVC下的異步Action的定義和執(zhí)行原理,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-12-12.NET連接數(shù)據(jù)庫以及基本的增刪改查操作教程
這篇文章主要給大家介紹了關(guān)于.NET連接數(shù)據(jù)庫以及基本的增刪改查操作教程的相關(guān)資料,對于剛?cè)腴T的新手們來說是個(gè)很好的入門教程,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-01-01.net indexOf(String.indexOf 方法)
字符串的IndexOf()方法搜索在該字符串上是否出現(xiàn)了作為參數(shù)傳遞的字符串,如果找到字符串,則返回字符的起始位置 (0表示第一個(gè)字符,1表示第二個(gè)字符依此類推)如果說沒有找到則返回 -12012-10-10輕量級(jí)ORM框架Dapper應(yīng)用之Dapper支持存儲(chǔ)過程
這篇文章介紹了Dapper支持使用存儲(chǔ)過程的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03CorFlags.exe檢查.NET程序平臺(tái)目標(biāo)(Platform Target)的工具
.NET Framework SDK中的一個(gè)工具程序: CorFlags.exe。CorFlags.exe不但可查詢.NET組件的平臺(tái)目標(biāo)設(shè)定,甚至能直接修改設(shè)定,省去重新編譯的工夫。2013-02-02Visual studio 2017添加引用時(shí)報(bào)錯(cuò)未能正確加載ReferenceManagerPackage包的解決方法
這篇文章主要介紹了VS2017添加引用時(shí)報(bào)錯(cuò)未能正確加載ReferenceManagerPackage包的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04JWT + ASP.NET MVC時(shí)間戳防止重放攻擊詳解
這篇文章主要給大家介紹了關(guān)于JWT + ASP.NET MVC時(shí)間戳防止重放攻擊發(fā)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07