Get the App
SLTechnology News&Howtos  ›  Development  › 

How to develop users to modify data in MVC4 website

Shulou Source: shulou.com Published: 2022-06-03 12:56:16 10月04日 Update

MVC4 production site how to develop users to modify information, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.

I. users

1.1 user Registration

1.2 user login

1.3 change password

1.4 revision of data

After the user has successfully logged in, you have to jump to a page, which is temporarily called user Center. Add [default] action to [UserController]

[UserAuthorize] public ActionResult Default () {userRsy = new UserRepository (); var _ user = userRsy.Find (UserName); return View (_ user);}

Add corresponding strongly typed views

@ model Ninesky.Models.User@ {ViewBag.Title = "homepage"; Layout = "~ / Views/Layout/_User.cshtml";} here the left navigation list where you are now: user homepage @ Model.UserName @ Model.GroupId

Now to make the left navigation list, right-click on the User folder of the view to create a new detail view PartialPersonalNav

@ Html.ActionLink ("user home page", "Default", "User") @ Html.ActionLink ("modify information", "ChangeInfo", "User") @ Html.ActionLink ("change password", "ChangePassword", "User") @ Html.ActionLink ("exit system", "Logout", "User")

Replace the "left navigation list here" in default.cshtml with @ Html.Partial ("PartialPersonalNav"). Open in the browser and the navigation list is displayed.

Now it is time to modify the user profile. Add [ChangeInfo] action to [UserController]

[UserAuthorize] public ActionResult ChangeInfo () {userRsy = new UserRepository (); var _ user = userRsy.Find (UserName); return View (_ user);}

Add action for processing modified 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 = "data modified successfully", Details = "you have successfully modified data!" , DwellTime = 5, NavigationName = "user home page", NavigationUrl = Url.Action ("Default", "User")}; return RedirectToAction ("UserNotice", "Prompt", _ n) } else {Error _ e = new Error {Title = "failed to modify the data", Details = "the updated data could not be saved to the database when modifying the user's data", Cause = "system error", Solution = Server.UrlEncode ("return the modification page, enter the correct information and contact the webmaster again")}; return RedirectToAction ("UserError", "Prompt", _ e) }} else {ModelState.AddModelError ("Password", "wrong password!") ; return View ();}}

Aciton right click to strengthen the type view, modify the view to automatically generate code, after completion. As follows:

@ model Ninesky.Models.User@ {ViewBag.Title = "modify personal data"; Layout = "~ / Views/Layout/_User.cshtml" } @ Html.Partial ("PartialPersonalNav") your current location: user homepage @ using (Html.BeginForm ()) {@ Html.ValidationSummary (true) revision @ Html.HiddenFor (model = > model.UserId) @ Html.LabelFor (model = > model.UserName) @ Html.DisplayFor (model = > model.UserName) @ Html.LabelFor (model = > model.Password) @ Html .password ("Password") @ Html.ValidationMessageFor (model = > model.Password) enter the correct password to change the data. @ Html.LabelFor (model = > model.Gender) @ Html.RadioButtonFor (model = > model.Gender, 0) male @ Html.RadioButtonFor (model = > model.Gender, 1) female @ Html.RadioButtonFor (model = > model.Gender) 2) Secret @ Html.LabelFor (model = > model.Email) @ Html.EditorFor (model = > model.Email) @ Html.ValidationMessageFor (model = > model.Email) @ Html.DisplayDescriptionFor (model = > model.Email) @ Html.LabelFor (model = > model.QQ) @ Html.EditorFor (model = > model.QQ) @ Html.ValidationMessageFor (model = > model.QQ) @ Html.DisplayDescriptionFor (model = > model.QQ) @ Html.LabelFor (model = > model.Tel) @ Html.EditorFor (model = > model.Tel) @ Html.ValidationMessageFor (model = > model.Tel) @ Html.DisplayDescriptionFor (model = > model.Tel) @ Html.LabelFor (model = > model.Address) @ Html.EditorFor (model = > model.Address) @ Html.ValidationMessageFor (model = > model.Address) @ Html .DisplayDescriptionFor (model = > model.Address) @ Html.LabelFor (model = > model.PostCode) @ Html.EditorFor (model = > model.PostCode) @ Html.ValidationMessageFor (model = > model.PostCode) @ Html.DisplayDescriptionFor (model = > model.PostCode)} @ section Scripts {@ Scripts.Render ("~ / bundles/jqueryval")}

Run it and see

Enter the data to test it. Can be saved to the database normally.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Tags: Information user view home page password navigation success input website location information data database user information type system error page help create website Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Shulou Tech Info OPPO Reno Apple MariaDB