您好,登錄后才能下訂單哦!
MVC4制作網站中怎樣開發用戶修改資料,很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
一、用戶
1.1用戶注冊
1.2用戶登錄
1.3修改密碼
1.4修改資料
在用戶登陸成功后要跳轉到一個頁面,暫且叫做用戶中心吧。在【UserController】添加[default] action
[UserAuthorize] public ActionResult Default() { userRsy = new UserRepository(); var _user = userRsy.Find(UserName); return View(_user); }
添加相應對應強類型視圖
@model Ninesky.Models.User @{ ViewBag.Title = "首頁"; Layout = "~/Views/Layout/_User.cshtml"; } <div class="leftnav">這里左側導航列表</div> <div class="workspace"> <div class ="Nav">您現在的位置: 用戶首頁</div> <div>@Model.UserName <br /> @Model.GroupId </div> </div>
現在要把左側導航列表做出來,在視圖的User文件夾上點右鍵新建局部視圖PartialPersonalNav
<ul> <li>@Html.ActionLink("用戶首頁","Default","User")</li> <li>@Html.ActionLink("修改信息","ChangeInfo","User")</li> <li>@Html.ActionLink("修改密碼","ChangePassword","User")</li> <li>@Html.ActionLink("退出系統","Logout","User")</li> </ul>
將default.cshtml中“這里左側導航列表”替換為@Html.Partial("PartialPersonalNav")。瀏覽器中打開,導航列表顯示出來了。
現在開始做修改用戶資料了。在【UserController】添加[ChangeInfo] action
[UserAuthorize] public ActionResult ChangeInfo() { userRsy = new UserRepository(); var _user = userRsy.Find(UserName); return View(_user); }
添加修改資料的處理 action
[HttpPost] [UserAuthorize] public ActionResult ChangeInfo(User user) { userRsy = new UserRepository(); if(userRsy.Authentication(UserName,Ninesky.Common.Text.Sha256(user.Password))==0) { var _user = userRsy.Find(UserName); _user.Gender = user.Gender; _user.Email = user.Email; _user.QQ = user.QQ; _user.Tel = user.Tel; _user.Address = user.Address; _user.PostCode = user.PostCode; if (userRsy.Update(_user)) { Notice _n = new Notice { Title = "修改資料成功", Details = "您已經成功修改資料!", DwellTime = 5, NavigationName = "用戶首頁", NavigationUrl = Url.Action("Default", "User") }; return RedirectToAction("UserNotice", "Prompt", _n); } else { Error _e = new Error { Title = "修改資料失敗", Details = "在修改用戶資料時時,更新的資料未能保存到數據庫", Cause = "系統錯誤", Solution = Server.UrlEncode("<li>返回<a href='" + Url.Action("ChangeInfo", "User") + "'>修改資料</a>頁面,輸入正確的信息后重新操作</li><li>聯系網站管理員</li>") }; return RedirectToAction("UserError", "Prompt", _e); } } else { ModelState.AddModelError("Password","密碼錯誤!"); return View(); } }
aciton上右鍵添加強類型視圖,修改視圖里自動生成代碼,完成后。如下:
@model Ninesky.Models.User @{ ViewBag.Title = "修改個人資料"; Layout = "~/Views/Layout/_User.cshtml"; } <div class="leftnav">@Html.Partial("PartialPersonalNav")</div> <div class="workspace"> <div class="Nav">您現在的位置: 用戶首頁</div> <div> @using (Html.BeginForm()) { @Html.ValidationSummary(true) <fieldset> <legend>修改資料</legend> @Html.HiddenFor(model => model.UserId) <ul> <li> <div class="editor-label"> @Html.LabelFor(model => model.UserName) </div> <div class="editor-field"> @Html.DisplayFor(model => model.UserName) </div> </li> <li> <div class="editor-label"> @Html.LabelFor(model => model.Password) </div> <div class="editor-field"> @Html.Password("Password") @Html.ValidationMessageFor(model => model.Password) 輸入正確的密碼才能修改資料。 </div> </li> <li> <div class="editor-label"> @Html.LabelFor(model => model.Gender) </div> <div class="editor-field"> @Html.RadioButtonFor(model => model.Gender, 0) 男 @Html.RadioButtonFor(model => model.Gender, 1) 女 @Html.RadioButtonFor(model => model.Gender, 2) 保密 </div> </li> <li> <div class="editor-label"> @Html.LabelFor(model => model.Email) </div> <div class="editor-field"> @Html.EditorFor(model => model.Email) @Html.ValidationMessageFor(model => model.Email) @Html.DisplayDescriptionFor(model => model.Email) </div> </li> <li> <div class="editor-label"> @Html.LabelFor(model => model.QQ) </div> <div class="editor-field"> @Html.EditorFor(model => model.QQ) @Html.ValidationMessageFor(model => model.QQ) @Html.DisplayDescriptionFor(model => model.QQ) </div> </li> <li> <div class="editor-label"> @Html.LabelFor(model => model.Tel) </div> <div class="editor-field"> @Html.EditorFor(model => model.Tel) @Html.ValidationMessageFor(model => model.Tel) @Html.DisplayDescriptionFor(model => model.Tel) </div> </li> <li> <div class="editor-label"> @Html.LabelFor(model => model.Address) </div> <div class="editor-field"> @Html.EditorFor(model => model.Address) @Html.ValidationMessageFor(model => model.Address) @Html.DisplayDescriptionFor(model => model.Address) </div> </li> <li> <div class="editor-label"> @Html.LabelFor(model => model.PostCode) </div> <div class="editor-field"> @Html.EditorFor(model => model.PostCode) @Html.ValidationMessageFor(model => model.PostCode) @Html.DisplayDescriptionFor(model => model.PostCode) </div> </li> <li><input type="submit" value="修改" /></li> </ul> </fieldset> } </div> </div> @section Scripts { @Scripts.Render("~/bundles/jqueryval") }
運行一下看
輸入資料測試一下。能夠正常保存到數據庫。
看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業資訊頻道,感謝您對億速云的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。