Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to modify and delete user data in the development of ASP.NETMVC5 website

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

What this article shares with you is about how to modify and delete user data in the development of the ASP.NETMVC5 website. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article.

This time, it mainly realizes the modification and deletion of the user data of the management background interface, and modifying the user data and roles is a function that is often used, but the deletion of users is relatively rare, so it is still on the seat for the sake of functional integrity. Two action "Modify" and "Delete" are mainly used.

1. Modification of user data (Modify)

This feature is divided into two parts:

Public ActionResult Modify (int id) is used to display user information

[httppost]

Public ActionResult Modify (FormCollection form) users receive the information from the front desk and modify it.

1. Display user information

/ / modify user information / user primary key / partial view public ActionResult Modify (int id) {/ / role list var _ roles = new RoleManager () .FindList (); List _ listItems = new List (_ roles.Count ()) Foreach (var _ role in _ roles) {_ listItems.Add (new SelectListItem () {Text = _ role.Name, Value = _ role.RoleID.ToString ()});} ViewBag.Roles = _ listItems; / / end of role list return PartialView (userManager.Find (id));}

This action has a parameter id that receives the incoming user ID, queries the action for role information, passes it to the view using viewBage, and passes the user model back to the partial view through return PartialView (userManager.Find (id)).

The view code is as follows:

@ model Ninesky.Core.User@using (Html.BeginForm ()) {@ Html.AntiForgeryToken () @ Html.ValidationSummary (true, ", new {@ class =" text-danger "}) @ Html.HiddenFor (model = > model.UserID) @ Html.LabelFor (model = > model.RoleID, htmlAttributes: new {@ class =" control-label col-md-2 "}) @ Html.DropDownListFor (model = > model.RoleID, (IEnumerable) ViewBag.Roles New {@ class = "form-control"}) @ Html.ValidationMessageFor (model = > model.RoleID, "", new {@ class = "text-danger"}) @ Html.LabelFor (model = > model.Username, htmlAttributes: new {@ class = "control-label col-md-2"}) @ Html.EditorFor (model = > model.Username, new {htmlAttributes = new {@ class = "form-control") Disabled = "disabled"}}) @ Html.ValidationMessageFor (model = > model.Username, ", new {@ class =" text-danger "}) @ Html.LabelFor (model = > model.Name, htmlAttributes: new {@ class =" control-label col-md-2 "}) @ Html.EditorFor (model = > model.Name, new {htmlAttributes = new {@ class =" form-control "}}) @ Html.ValidationMessageFor (model = > model.Name," New {@ class = "text-danger"}) @ Html.LabelFor (model = > model.Sex, htmlAttributes: new {@ class = "control-label col-md-2"}) @ Html.RadioButtonFor (model = > model.Sex, 1) male @ Html.RadioButtonFor (model = > model.Sex, 0) female @ Html.RadioButtonFor (model = > model.Sex, 2) secrecy @ Html.ValidationMessageFor (model = > model.Sex, " New {@ class = "text-danger"}) @ Html.LabelFor (model = > model.Password, htmlAttributes: new {@ class = "control-label col-md-2"}) @ Html.EditorFor (model = > model.Password, new {htmlAttributes = new {@ class = "form-control"}}) @ Html.ValidationMessageFor (model = > model.Password, "" New {@ class = "text-danger"}) @ Html.LabelFor (model = > model.Email, htmlAttributes: new {@ class = "control-label col-md-2"}) @ Html.EditorFor (model = > model.Email, new {htmlAttributes = new {@ class = "form-control"}}) @ Html.ValidationMessageFor (model = > model.Email, "" New {@ class = "text-danger"}) @ Html.LabelFor (model = > model.LastLoginTime, htmlAttributes: new {@ class = "control-label col-md-2"}) @ Html.EditorFor (model = > model.LastLoginTime, new {htmlAttributes = new {@ class = "form-control", disabled = "disabled"}) @ Html.ValidationMessageFor (model = > model.LastLoginTime, "" New {@ class = "text-danger"}) @ Html.LabelFor (model = > model.LastLoginIP, htmlAttributes: new {@ class = "control-label col-md-2"}) @ Html.EditorFor (model = > model.LastLoginIP, new {htmlAttributes = new {@ class = "form-control", disabled = "disabled"}) @ Html.ValidationMessageFor (model = > model.LastLoginIP, "" New {@ class = "text-danger"}) @ Html.LabelFor (model = > model.RegTime, htmlAttributes: new {@ class = "control-label col-md-2"}) @ Html.EditorFor (model = > model.RegTime, new {htmlAttributes = new {@ class = "form-control", disabled = "disabled"}}) @ Html.ValidationMessageFor (model = > model.RegTime, ", new {@ class =" text-danger "})}

2. Background processing of modifying user data

[HttpPost] [ValidateAntiForgeryToken] public ActionResult Modify (int id,FormCollection form) {Response _ resp = new Auxiliary.Response (); var _ user = userManager.Find (id); if (TryUpdateModel (_ user, new string [] {"RoleID", "Name", "Sex", "Email"})) {if (_ user = = null) {_ resp.Code = 0 _ resp.Message = "user does not exist, may have been deleted, please refresh and try again";} else {if (_ user.Password! = form ["Password"] .ToString ()) _ user.Password = Security.SHA256 (form ["Password"]. ToString ()); _ resp = userManager.Update (_ user);}} else {_ resp.Code = 0; _ resp.Message = General.GetModelErrorString (ModelState) } return Json (_ resp);}

This method has two parameters, id and FormCollection form. The reason why you do not use User to do the model directly is because user will receive all the data from the foreground. Here, I do not want to allow the user name to be modified, so I use TryUpdateModel binding to allow the user to modify the attributes in the method. TryUpdateModel also records errors in ModelState when binding fails. You can use the custom method GetModelErrorString to get the error information and feed it back to the view.

2. Front desk display and processing

Open the Index view to find the table initialization method, format the column "Username" so that it shows a connection, the red line section of the code.

To make it look like this, the modify dialog box can be displayed when the user clicks the link.

The pop-up window and the js code sent to the server are written to the onLoadSuccess method of the table

OnLoadSuccess: function () {/ / modify $("a [data-method='Modify']") .click (function () {var id = $(this) .attr ("data-value"); var modifyDialog = new BootstrapDialog ({title: "modify user", message: function (dialog) {var $message = $(''); var pageToLoad = dialog.getData ('pageToLoad'); $message.load (pageToLoad)) Return $message }, data: {'pageToLoad':' @ Url.Action ("Modify") /'+ id}, buttons: [{icon: "glyphicon glyphicon-plus", label: "Save", action: function (dialogItself) {$.post ($("form"). Attr ("action"), $("form"). SerializeArray () Function (data) {if (data.Code = = 1) {BootstrapDialog.show ({message: data.Message, buttons: [{icon: "glyphicon glyphicon-ok", label: "OK", action: function (dialogItself) {$table.bootstrapTable ("refresh") DialogItself.close (); modifyDialog.close ();}}]});} else BootstrapDialog.alert (data.Message);}, "json"); $("form") .validate () }, {icon: "glyphicon glyphicon-remove", label: "close", action: function (dialogItself) {dialogItself.close ();}}]}); modifyDialog.open ();}); / / end of modification}

The display effect is as follows

2. Delete users

Add and delete methods in UserController

/ / delete / user ID / [HttpPost] public ActionResult Delete (int id) {return Json (userManager.Delete (id));}

Open the Index view to find the table initialization method, add the Action column to format the column to display a delete button, the red box section of the code.

Foreground display effect

Then write the js code that deletes the user after the js code that you just wrote to modify the user information in the onLoadSuccess method of the table

/ / modify the end / / delete button $("a [data-method='Delete']") .click (function () {var id = $(this) .attr ("data-value"); BootstrapDialog.confirm ("are you sure you want to delete" + $(this). Parent (). Parent (). Find ("td"). Eq (3). Text () +?\ nIt is recommended not to delete users as much as possible. " , function (result) {if (result) {$.post ("@ Url.Action (" Delete "," User ")", {id: id}, function (data) {if (data.Code = = 1) {BootstrapDialog.show ({message: "user deleted successfully", buttons: [{icon: "glyphicon glyphicon-ok") Label: "OK", action: function (dialogItself) {$table.bootstrapTable ("refresh") DialogItself.close ();}}]});} else BootstrapDialog.alert (data.Message);}, "json");}});}); / / end of delete button}}); / / end of table

Foreground display effect

The above is how to modify and delete user data in the development of ASP.NETMVC5 website. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report