Chapter 2 user modification materials 2.4, mvc42.4
I. Users
1.1 User Registration
1.2 User Logon
1.3 Change Password
1.4 modify materials
After a user logs on to the console, you must go to a page called user center. Add [default] action in [UserController]
[UserAuthorize] public ActionResult Default() { userRsy = new UserRepository(); var _user = userRsy.Find(UserName); return View(_user); }
Add corresponding strong view
@ Model Ninesky. Models. User @ {ViewBag. Title = "Homepage"; Layout = "~ /Views/Layout/_ User. cshtml ";} <div class = "leftnav"> here is the left navigation list </div> <div class = "workspace"> <div class = "Nav"> your current location: user homepage </div> <div> @ Model. userName <br/> @ Model. groupId </div>
Right-click the User folder in the view to create the local view PartialPersonalNav.
<Ul> <li> @ Html. actionLink ("User Homepage", "Default", "User") </li> <li> @ Html. actionLink ("Modify information", "ChangeInfo", "User") </li> <li> @ Html. actionLink ("Change Password", "ChangePassword", "User") </li> <li> @ Html. actionLink ("Exit System", "Logout", "User") </li> </ul>
Replace "here left navigation list" in default. cshtml with @ Html. Partial ("PartialPersonalNav "). Open in the browser, and the navigation list is displayed.
Now we are modifying user information. Add [ChangeInfo] action in [UserController]
[UserAuthorize] public ActionResult ChangeInfo() { userRsy = new UserRepository(); var _user = userRsy.Find(UserName); return View(_user); }
Add action for modifying data
[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 = "you have modified the materials successfully! ", DwellTime = 5, NavigationName =" User Homepage ", NavigationUrl = Url. action ("Default", "User")}; return RedirectToAction ("UserNotice", "Prompt", _ n );} else {Error _ e = new Error {Title = "failed to modify data", Details = "when modifying user data, the updated data cannot be saved to the Database ", cause = "system error", Solution = Server. urlEncode ("<li> Returns <a href = '" + Url. action ("ChangeInfo", "User") + "'> modify materials </a> page, enter the correct information and click </li> <li> contact the website administrator </li> ")}; return RedirectTo Action ("UserError", "Prompt", _ e) ;}} else {ModelState. AddModelError ("Password", "Incorrect Password! "); Return View ();}}
Right-click aciton to add a strong-type view, and modify the view to automatically generate code. As follows:
@ Model Ninesky. Models. User @ {ViewBag. Title = "Modify Profile"; Layout = "~ /Views/Layout/_ User. cshtml ";}< div class =" leftnav "> @ Html. partial ("PartialPersonalNav") </div> <div class = "workspace"> <div class = "Nav"> your current location: user homepage </div> <div> @ using (Html. beginForm () {@ Html. validationSummary (true) <fieldset> <legend> modify data </legend> @ Html. hiddenFor (model => model. userId) <ul> <li> <div class = "editor-label"> @ Html. labelFor (model => model. userName) </div> <div class = "editor-field"> @ Ht Ml. displayFor (model => model. userName) </div> </li> <div class = "editor-label"> @ Html. labelFor (model => model. password) </div> <div class = "editor-field"> @ Html. password ("Password") @ Html. validationMessageFor (model => model. password) enter the correct Password to modify the information. </Div> </li> <div class = "editor-label"> @ Html. labelFor (model => model. gender) </div> <div class = "editor-field"> @ Html. radioButtonFor (model => model. gender, 0) Male @ Html. radioButtonFor (model => model. gender, 1) female @ Html. radioButtonFor (model => model. gender, 2) Confidentiality </div> </li> <div class = "editor-label"> @ Html. labelFor (model => model. email) </div> <div class = "editor-field"> @ Html. editorFor (mod El => model. email) @ Html. validationMessageFor (model => model. email) @ Html. displayDescriptionFor (model => model. email) </div> </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> <div class = "editor-labe L "> @ 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> <div class = "editor-label"> @ Html. labelFor (model => model. address) </div> <div class = "editor-field"> @ Html. editorFor (model => model. address) @ Html. validationMessageFor (model => mo Del. address) @ Html. displayDescriptionFor (model => model. address) </div> </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> <input type = "submit" value = "modify"/> </li> </ul> </field Set >}</div> </div> @ section Scripts {@ Scripts. Render ("~ /Bundles/jqueryval ")}
Run it.
Enter the required information to test. Can be saved to the database normally.
==============================================
At the beginning, I learned MVC. In addition, I had limited expression ability and did not write a rough plan in advance. It was very poor and messy. Now I want to write where I want to write it. Sometimes I find that I can't write it before, and I want to change the code I wrote before.
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.