Seven user functions of MVC5 website development 2. 1 modify and delete user data, mvc5.1

Source: Internet
Author: User

Seven user functions of MVC5 website development 2. 1 modify and delete user data, mvc5.1

This time, we mainly implement the modification and deletion of user data on the management background interface. modifying user data and roles is a frequently used function, but there are few cases of Deleting Users, so we can continue to improve functional integrity. It mainly uses two actions: "Modify" and "Delete ".

Directory

Overview of MVC5 website development

Create a project for MVC5 website development 2

MVC5 website development 3 data storage layer function implementation

Architecture and basic functions of the four business logic layers of MVC5 website development

5-display layer architecture of MVC5 website development

6 administrators of MVC5 website development 1. logon, verification, and logout

6 administrators of MVC5 website development 2. add, delete, Reset Password, Change Password, list browsing

Seven user functions of MVC5 website development 1. Role background management

Seven users in MVC5 website development function 2 adding and browsing users

 

 

1. Modify user data (Modify)

This function has two parts:

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

[Httppost]

The public ActionResult Modify (FormCollection form) user receives and modifies the information sent from the foreground.

1. Display User Information

/// <Summary> /// modify user information /// </summary> /// <param name = "id"> User primary key </param> /// <returns> segment view </returns> public ActionResult Modify (int id) {// role list var _ roles = new RoleManager (). findList (); List <SelectListItem> _ listItems = new List <SelectListItem> (_ roles. count (); foreach (var _ role in _ roles) {_ listItems. add (new SelectListItem () {Text = _ role. name, Value = _ role. roleID. toString ()});} ViewBag. roles = _ listItems; // return PartialView (userManager. find (id ));}

This action has a parameter id that receives the input user id, queries role information in the action, transmits the role information to the view using viewBage, and returns PartialView (userManager. find (id) transmits the user model to the view and returns the segment view.

The view code is as follows:

@ Model Ninesky. core. user @ using (Html. beginForm () {@ Html. antiForgeryToken () <div class = "form-horizontal"> @ Html. validationSummary (true, "", new {@ class = "text-danger"}) @ Html. hiddenFor (model => model. userID) <div class = "form-group"> @ Html. labelFor (model => model. roleID, htmlAttributes: new {@ class = "control-label col-md-2"}) <div class = "col-md-10"> @ Html. dropDownListFor (model => model. roleID, (IEnumerable <SelectListItem>) ViewBag. roles, new {@ class = "form-control"}) @ Html. validationMessageFor (model => model. roleID, "", new {@ class = "text-danger"}) </div> <div class = "form-group"> @ Html. labelFor (model => model. username, htmlAttributes: new {@ class = "control-label col-md-2"}) <div class = "col-md-10"> @ Html. editorFor (model => model. username, new {htmlAttributes = new {@ class = "form-control", disabled = "disabled"}) @ Html. validationMessageFor (model => model. username, "", new {@ class = "text-danger"}) </div> <div class = "form-group"> @ Html. labelFor (model => model. name, htmlAttributes: new {@ class = "control-label col-md-2"}) <div class = "col-md-10"> @ Html. editorFor (model => model. name, new {htmlAttributes = new {@ class = "form-control"}) @ Html. validationMessageFor (model => model. name, "", new {@ class = "text-danger"}) </div> <div class = "form-group"> @ Html. labelFor (model => model. sex, htmlAttributes: new {@ class = "control-label col-md-2"}) <div class = "col-md-10"> @ Html. radioButtonFor (model => model. sex, 1) Male @ Html. radioButtonFor (model => model. sex, 0) Female @ Html. radioButtonFor (model => model. sex, 2) Confidentiality @ Html. validationMessageFor (model => model. sex, "", new {@ class = "text-danger"}) </div> <div class = "form-group"> @ Html. labelFor (model => model. password, htmlAttributes: new {@ class = "control-label col-md-2"}) <div class = "col-md-10"> @ Html. editorFor (model => model. password, new {htmlAttributes = new {@ class = "form-control"}) @ Html. validationMessageFor (model => model. password, "", new {@ class = "text-danger"}) </div> <div class = "form-group"> @ Html. labelFor (model => model. email, htmlAttributes: new {@ class = "control-label col-md-2"}) <div class = "col-md-10"> @ Html. editorFor (model => model. email, new {htmlAttributes = new {@ class = "form-control"}) @ Html. validationMessageFor (model => model. email, "", new {@ class = "text-danger"}) </div> <div class = "form-group"> @ Html. labelFor (model => model. lastLoginTime, htmlAttributes: new {@ class = "control-label col-md-2"}) <div class = "col-md-10"> @ Html. editorFor (model => model. lastLoginTime, new {htmlAttributes = new {@ class = "form-control", disabled = "disabled"}) @ Html. validationMessageFor (model => model. lastLoginTime, "", new {@ class = "text-danger"}) </div> <div class = "form-group"> @ Html. labelFor (model => model. lastLoginIP, htmlAttributes: new {@ class = "control-label col-md-2"}) <div class = "col-md-10"> @ Html. editorFor (model => model. lastLoginIP, new {htmlAttributes = new {@ class = "form-control", disabled = "disabled"}) @ Html. validationMessageFor (model => model. lastLoginIP, "", new {@ class = "text-danger"}) </div> <div class = "form-group"> @ Html. labelFor (model => model. regTime, htmlAttributes: new {@ class = "control-label col-md-2"}) <div class = "col-md-10"> @ Html. editorFor (model => model. regTime, new {htmlAttributes = new {@ class = "form-control", disabled = "disabled"}) @ Html. validationMessageFor (model => model. regTime, "", new {@ class = "text-danger"}) </div>}

2. Modify the background processing of user data

[HttpPost] [ValidateAntiForgeryToken] public ActionResult Modify (int id, FormCollection form) {Response _ resp = new aupoliciary. response (); var _ user = userManager. find (id); if (TryUpdateModel (_ user, new string [] {"RoleID", "Name", "Sex", "Email "})) {if (_ user = null) {_ resp. code = 0; _ resp. message = "the user does not exist and 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 that the User does not directly make the model is that the user will receive all the data in the foreground. Here, I do not want to allow the User name to be modified, therefore, you can use TryUpdateModel in the method to bind attributes that can be modified by users. TryUpdateModel will also record errors in ModelState when binding fails. You can use the custom method GetModelErrorString to get error information and feedback it to the view.

2. Front-end display and Processing

Open the Index view and find the table initialization method. format the column "Username" to display a connection and the Code Red Line Section.

OnLoadSuccess: function () {// Modify $ ("a [data-method = 'modify']"). click (function () {var id = $ (this ). attr ("data-value"); var modifyDialog = new BootstrapDialog ({title: "<span class = 'glyphicon glyphicon-user'> </span> modify user", message: function (dialog) {var $ message = $ ('<div> </div>'); var pageToLoad = dialog. getData ('pagetoload'); $ message. load (pageToLoad); return $ message;}, data: {'pagetoload': '@ Url. action ("Modify")/'+ id}, buttons: [{icon: "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-OK", label: "OK", action: function (dialogItself) {$ table. bootstrapTable ("refresh"); dialogItself. close (); modifyDialog. close () ;}}]});} else BootstrapDialog. alert (data. message) ;}, "json"); $ ("form "). validate () ;},{ icon: "glyphicon-remove", label: "disabled", action: function (dialogItself) {dialogItself. close () ;}}]}); modifyDialog. open () ;}); // modification ended}

The display effect is as follows:

/// <Summary> /// Delete /// </summary> /// <param name = "id"> User id </param> /// <returns> </returns> [HttpPost] public ActionResult Delete (int id) {return Json (userManager. delete (id ));}

 

Open the Index view, find the table initialization method, and add the "operation" column to display a delete button. The code is highlighted in red.

// Modify 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 () +? \ N do not 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-OK", label: "OK", action: function (dialogItself) {$ table. bootstrapTable ("refresh"); dialogItself. close () ;}}]});} else BootstrapDialog. alert (data. message) ;}, "json") ;}}) ;}; // delete button ended }}); // table ended

Foreground display

 

========================================================== =

See http://www.cnblogs.com/mzwhj/p/5729848.html for code downloads

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.