In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
In this issue, the editor will bring you about how to achieve the background management of user roles in the development of ASP.NETMVC5 website. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.
Role is a function in the website, which is used to distinguish the types of users and divide the permissions of users. This time, role list browsing, role addition, role modification and role deletion are realized.
I. Business logic layer
1. Role model
Ninesky.Core [right]-> add-> class, and enter the class name Role.
Reference System.ComponentModel.DataAnnotations Namespace
Using System.ComponentModel.DataAnnotations;namespace Ninesky.Core {/ role / public class Role {[Key] public int RoleID {get; set;} / name / [Required (ErrorMessage = "must be entered {0}")] [StringLength (20 min length = 2, ErrorMessage = "{2}-{1} characters")] [Display (Name = "name")] public string Name {get Set;} / description / / [StringLength (1000, ErrorMessage = "{0} must be less than {1} characters")] [Display (Name = "description")] public string Description {get; set;}
2. Add table mapping
Open Ninesky.Core/NineskyContext.cs and add Role table mapping
3. Migrate data
1) enable data migration
Go to [Toolbar]-> [tools]-> NuGet package Manager-> package Manager console.
Enter the command Enable-Migrations enter to enable data migration for Ninesk.Core.
Open the Ninesky.Core/Migrations/Configuration.cs file
Change AutomaticMigrationsEnabled = false; to AutomaticMigrationsEnabled = ture; to enable automatic migration.
2), update the data table
Run the command Update-Database. Error: There is already an object named 'Administrators' in the database.
This is because the data migration is enabled after the husband becomes the Administrators table. The view failed to create the Administrators table while updating the table.
Open Server Explorer and select Administrators [right]-> Delete as shown in the figure.
Run Update-Database again after the deletion is successful, and the execution is successful.
Because the administrator account was also deleted when deleting the table just now, remember to open the Administrators table to add an administrator account, and remember that the password can be entered into jZae727K08KaOmKSgOaGzww/XVqGr/PKEgIMkjrcbJI=, which is a 123456 encrypted string.
4. Role management
Ninesky.Core [right]-> add-> class, enter the class name RoleManager, which inherits from BaseManager
Using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace Ninesky.Core {/ role Management / public class RoleManager:BaseManager {}}
II. Display layer
Ninesky.Web/Areas/Control/Controllers [right]-> add-> Controller. Select MVC5 Controller-null, enter the controller name RoleController.
Introduce the namespace Ninesky.Core into the controller
Add the variable private RoleManager roleManager = new RoleManager ()
Add authentication to the controller [AdminAuthorize]
1. Message prompt
During the operation, we often need to prompt for the success, failure and error of the operation, so the model class Prompt is specially designed to prompt.
1), add a class
Ninesky.Web/Models [right]-> add-> Class enter the class name Prompt
The copy code is as follows:
Using System.Collections.Generic;namespace Ninesky.Web.Models {/ prompt / public class Prompt {/ title / public string Title {get; set;} / message / public string Message {get; set;} / button group / public List Buttons {get; set;}
2. Introduce the namespace of the class into the controller
Reference the namespace Ninesky.Web.Models in Rolecontroller.
3. Add a view
In Ninesky.Web/Areas/Control/Views/Shared [right]-> add-> View
@ model Ninesky.Web.Models.Prompt@ {ViewBag.Title = Model.Title;} @ section SideNav {@ Html.Partial ("SideNavPartialView")} @ Html.ActionLink ("Home", "Index", "Home") @ Model.Title @ Model.Title
@ Html.Raw (Model.Message)
@ if (Model.Buttonsinvalid null & & Model.Buttons.Count > 0) {
@ foreach (var item in Model.Buttons) {@ Html.Raw (item+ "")}
}
2. List of administrators
1), return list method (Json mode)
Add the method ListJson () to the control and return the type JsonResoult
/ list [Json] / public JsonResult ListJson () {return Json (roleManager.FindList ());}
2. Add role home page view
Add a view in the index () method [right]->
@ {ViewBag.Title = "role Management" } @ section SideNav {@ Html.Partial ("SideNavPartialView")} @ Html.ActionLink ("Home", "Index", "Home") @ Html.ActionLink ("user Management", "Index", "User") @ Html.ActionLink ("role Management", "Index" "Role") @ section style {@ Styles.Render ("~ / Content/bootstrapplugincss")} @ section scripts {@ Scripts.Render ("~ / bundles/jqueryval") @ Scripts.Render ("~ / bundles/bootstrapplugin") $(document) .ready (function () {/ / form var $table = $('# admingrid') $table.bootstrapTable ({showRefresh: true, showColumns: true, showFooter: true, method: "post", url: "@ Url.Action (" ListJson ")", columns: [{title: "ID", field: "RoleID"}, {title: "name", field: "Name", formatter: function (value, row, index) {return "+ value +"}, {title: "description" Field: "Description"}, {title: "Operation", field: "RoleID", formatter: function (value) {return "delete"}], onLoadSuccess: function () {/ / delete button $("a [data-operation='deleterole']") .click (function () {var id = $(this) .attr ("data-value") BootstrapDialog.confirm ("are you sure you want to delete" + $(this). Parent (). Parent (). Find ("td"). Eq (1). Text () +? " , function (result) {if (result) {$.post ("@ Url.Action (" DeleteJson "," Role ")", {id: id}, function (data) {if (data.Code = = 1) {BootstrapDialog.show ({message: "role deleted successfully", buttons: [{icon: "glyphicon glyphicon-ok") Label: "OK", action: function (dialogItself) {$table.bootstrapTable ("refresh") DialogItself.close ();}}]});} else BootstrapDialog.alert (data.Message);}, "json");}});}); / / Delete button end}}); / / form end});}
3. Navigation view
The navigation view is displayed on the left side of the view to navigate the functions under the controller
Ninesky.Web/Areas/Control/Views/Role [right]-> add-> View
User Management @ Html.ActionLink ("role add", "Add", "Role") @ Html.ActionLink ("role Management", "Index", "Role")
4. Add roles
1), add method
Add an Add method to the controller
The copy code is as follows:
/ / add / public ActionResult Add () {return View ();}
2), add a view
Right-click on the method to add a view
@ model Ninesky.Core.Role@ {ViewBag.Title = "add roles" } @ section SideNav {@ Html.Partial ("SideNavPartialView")} @ Html.ActionLink ("Home", "Index", "Home") @ Html.ActionLink ("user Management", "Index", "User") @ Html.ActionLink ("role Management", "Index", "Role") add roles @ using (Html.BeginForm ()) {@ Html.AntiForgeryToken () @ Html.ValidationSummary (true, " 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.Description HtmlAttributes: new {@ class = "control-label col-md-2"}) @ Html.EditorFor (model = > model.Description, new {htmlAttributes = new {@ class = "form-control"}) @ Html.ValidationMessageFor (model = > model.Description, "", new {@ class = "text-danger"}) @ section Scripts {@ Scripts.Render ("~ / bundles/jqueryval")}
3) add the receiving and processing method of submitted data
Add the post method of Add method to the controller
[HttpPost] [ValidateAntiForgeryToken] public ActionResult Add (Role role) {if (ModelState.IsValid) {if (roleManager.Add (role). Code = = 1) {return View ("Prompt", new Prompt () {Title = "role added successfully", Message = "you have successfully added roles [" + role.Name+ "]", Buttons = new List () {"role Management", "continue to add"}}) } return View (role);}
5. Modification of administrator data
1), add method
Add the Modify method to the controller.
/ modify / RoleID / public ActionResult Modify (int id) {var _ role = roleManager.Find (id); if (_ role = = null) return View ("Prompt", new Prompt () {Title = "error", Message = "roles where ID is [" + id + "] does not exist", Buttons = new List () {"role Management"}); return View (_ role) }
2), add a view
Right-click to add a view in the method
The code is as follows:
@ model Ninesky.Core.Role@ {ViewBag.Title = Model.Name } @ section SideNav {@ Html.Partial ("SideNavPartialView")} @ Html.ActionLink ("Home", "Index", "Home") @ Html.ActionLink ("user Management", "Index", "User") @ Html.ActionLink ("role Management", "Index", "Role") modify @ using (Html.BeginForm ()) {@ Html.AntiForgeryToken () @ Html.ValidationSummary (true, " New {@ class = "text-danger"}) @ Html.HiddenFor (model = > model.RoleID) @ 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.Description, htmlAttributes: new {@ class = "control-label col-md-2"}) @ Html.EditorFor (model = > model.Description, new {htmlAttributes = new {@ class = "form-control"}}) @ Html.ValidationMessageFor (model = > model.Description, "" New {@ class = "text-danger"}) @ section Scripts {@ Scripts.Render ("~ / bundles/jqueryval")}
3) add the receiving and processing method of submitted data
Add a post submission processing method, Modify method, to the controller.
[HttpPost] [ValidateAntiForgeryToken] public ActionResult Modify (Role role) {if (ModelState.IsValid) {var _ resp = roleManager.Update (role) If (_ resp.Code = = 1) return View ("Prompt", new Prompt () {Title = "role modified successfully", Message = "you have successfully modified the role [" + role.Name + "]", Buttons = new List () {"role Management", "View", "add"})) Else return View ("Prompt", new Prompt () {Title = "failed to modify role", Message = "reason for failure: + _ resp.Message, Buttons = new List () {" role Management "," return "}});} else return View (role);}
6. Delete a role
Add the Modify method to the controller.
/ / delete [Json] / RoleID / [HttpPost] public JsonResult DeleteJson (int id) {return Json (roleManager.Delete (id));}
The role function is complete, press F5 browser to preview the effect.
The above is the editor for you to share the ASP.NETMVC5 website development how to achieve the background management of user roles, if you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.