A role is a feature in the Web site that distinguishes the user's type from the user's permissions, this time implementing role list browsing, role addition, role modification, and role deletion.
First, the business logic layer
1. Role model
Ninesky.core "Right Key"-> Add-> class, enter class name role.
Referencing System.ComponentModel.DataAnnotations namespaces
Using System.ComponentModel.DataAnnotations;
Namespace Ninesky.core
{
///<summary>
///roles
///</summary> public
class
role {
[Key] public
int Roleid {get; set;}
<summary>
///name
///</summary>
[Required (errormessage = "must enter {0}")]
[ Stringlength (20,minimumlength =2, errormessage = "{0} length is {2}-{1} characters")]
[Display (name = "Names")] public
string Name {get; set;}
<summary>
///Description
///</summary>
[Stringlength (1000, errormessage = "{0} must be less than {1} characters")]
[Display (Name = description]] public
string Description {get; set;}}
}
2. Add Table mappings
Open Ninesky.core/nineskycontext.cs, add role table mappings
3. Migrating data
1), Enable data migration
In the Toolbars-> Tools->nuget Package Manager-> the Package Manager console.
Enter the command enable-migrations carriage return to enable data migration for Ninesk.core.
Open Ninesky.core/migrations/configuration.cs File
Change automaticmigrationsenabled = False to automaticmigrationsenabled = Ture to enable automatic migration.
2), update the data table
Run command update-database. Hint Error: There is already an object named ' Administrators ' in the database.
This is because Sir is the data migration enabled after the Administrators table. The View Creation administrators table failed while updating the table.
Open Server Explorer, as shown in the figure Select Administrators "right"-> delete.
After successful deletion, run Update-database again and execute successfully.
Because just delete the table when the administrator account also deleted, remember to open the Administrators table to add an administrator account, remember the password can be entered jzae727k08kaomksgoagzww/xvqgr/pkegimkjrcbji= This is the 123456 encrypted string.
4. Role Management
Ninesky.core "Right Key"-> Add-> class, enter class name RoleManager, class inherits from Basemanager<role>
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Threading.Tasks;
Namespace Ninesky.core
{
///<summary>
///role Management
///</summary> public
class Rolemanager:basemanager<role>
{
}
}
Second, the exhibition layer
Ninesky.web/areas/control/controllers "Right Key"-> add-> Controller. Select MVC5 Controller-NULL, input controller name Rolecontroller.
Introducing namespace Ninesky.core into the controller ;
Add variable private rolemanager rolemanager = new rolemanager ();
To add authentication to the controller [Adminauthorize]
1. Message prompt
It is often necessary to prompt for the operation success, failure, and error when doing the operation, so the model class prompt is specially made a hint.
1), add Class
Ninesky.web/models "Right Key"-> Add-> Class input class name prompt
Copy Code code as follows:
Using System.collections.generic;namespace ninesky.web.models{///<summary>///hint///</summary> public Class Prompt {///<summary>///header///</summary> public string Title {get; set;}///<summary>/// ///</summary> public string Message {get; set;}///<summary>///button Group///</summary> public list< ;string> Buttons {get; set;}}}
2, the introduction of the class in the Controller namespace
Reference namespace Ninesky.Web.Models in Rolecontroller.
3. Add View
Add-> view in ninesky.web/areas/control/views/shared "right"->
@model Ninesky.Web.Models.Prompt
@{
viewbag.title = model.title;
}
@section sidenav{@Html. Partial ("Sidenavpartialview")}
<ol class= "breadcrumb" >
<li><span class= "Glyphicon glyphicon-home" ></span> @Html. ActionLink ("homepage", "Index", "Home") </li>
<li Class= "Active" > @Model .title</li>
</ol>
<div class= "Panel Panel-default" >
< Div class= "panel-heading" ><div class= "Panel-title" > @Model .title</div></div> <div
class= "Panel-body" >
<p> @Html Raw (model.message) </p>
@if (model.buttons!=null && Model.Buttons.Count > 0) {
<p>
@foreach (var item in model.buttons)
{
@Html. Raw (item+ "")
}
</p>
}
</div>
</div>
2, the Administrator list
1), return list method (JSON method)
Add Method Listjson () in control, return type Jsonresoult
<summary>
///List "Json"
///</summary>
///<returns></returns>
Public Jsonresult Listjson ()
{return
Json (Rolemanager.findlist ());
}
2. Add Role Home View
Add a view in the index () method "right Key"->
@{viewbag.title = "role Management";} @section sidenav{@Html. Partial ("Sidenavpartialview")} <ol class= "breadcrumb" > <li><span class= " Glyphicon glyphicon-home "></span> @Html. ActionLink (" homepage "," Index "," Home ") </li> <li>@ Html.ActionLink ("User Management", "index", "user") </li> <li class= "active" > @Html. ActionLink ("Role Management", "Index", "roles ") </li> </ol> <table id=" Admingrid "></table> @section style{@Styles. Render (" ~/content/ Bootstrapplugincss ")} @section scripts{@Scripts. Render (" ~/bundles/jqueryval ") @Scripts. Render (" ~/bundles/ Bootstrapplugin ") <script type=" Text/javascript "> $ (document). Ready (function () {//table var $table = $ (' #adm
Ingrid '); $table. bootstraptable ({showrefresh:true, showcolumns:true, Showfooter:true, Method: "Post", url: " @Url. Action ("Listjson"), columns: [{title: "ID", Field: "Roleid"}, {title: "Name", Field: "Name", Formatt Er:function (value, row, index{return "<a href= ' @Url. Action (" Modify "," role ")/" + row. Roleid + "' >" + value + "</a>"}, {title: "description", field: "Description"}, {title: "Action", Field: "Rolei D ", formatter:function (value) {return ' <a class= ' btn btn-sm btn-danger ' data-operation= ' deleterole ' data-value= '" + Value + ' > Delete </a> '}], 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: Delete role succeeded, buttons: [{icon: "Glyp Hicon Glyphicon-ok ", Label: OK, Action:function (dialogitself) {$table. bootstrapt
Able ("refresh"); Dialogitself.close ();
}
}]
}); else Bootstrapdialog.alert (data.
message);
}, "JSON");
}
});
});
Delete button End}});
Form end});
</script>}
3, Navigation view
Navigation view is displayed on the left side of the view, navigating the functions under the controller
Ninesky.web/areas/control/views/role "Right Key"-> add-> View
<div class= "Panel Panel-default" >
<div class= "panel-heading" >
<div class= "Panel-title" > <span class= "Glyphicon glyphicon-user" ></span> user management </div>
</div>
<div class= " Panel-body ">
<div class=" List-group ">
<div class=" List-group-item "><span class=" Glyphicon glyphicon-plus "></span> @Html. ActionLink (" Add "," Add "," role ") </div>
<div class=" List-group-item "><span class=" Glyphicon glyphicon-list "></span> @Html. ActionLink (" Role Management "," Index "," Role ") </div>
</div>
</div>
</div>
4. Add roles
1), Add method
Add the Add method to the controller
Copy Code code as follows:
<summary>///Add///</summary>///<returns></returns> public actionresult Add () {return Vie W (); }
2), add view
Right-click to add a view on a method
@model Ninesky.Core.Role @{viewbag.title = "Add Role";} @section sidenav{@Html. Partial ("Sidenavpartialview")} <ol class= "breadcrumb" > <li><span class= " Glyphicon glyphicon-home "></span> @Html. ActionLink (" homepage "," Index "," Home ") </li> <li>@
Html.ActionLink ("User Management", "index", "user") </li> <li> @Html. ActionLink ("Role Management", "Index", "roles") </li> <li class= "Active" > Add role </li> </ol> @using (Html.BeginForm ()) {@Html. AntiForgeryToken () <div cl ass= "Form-horizontal" > @Html. ValidationSummary (True, "", new {@class = "Text-danger"}) <div class= "Form-group" > @Html. labelfor (model => model. Name, htmlattributes:new {@class = "Control-label col-md-2"}) <div class= "col-md-10" > @Html. Editorfor (Mode L => model. Name, new {htmlattributes = new {@class = "Form-control"}}) @Html. validationmessagefor (model => model.
Name, "", new {@class = "Text-danger"}) </div> </div>
<div class= "Form-group" > @Html. Labelfor (model => model. Description, htmlattributes:new {@class = "Control-label col-md-2"}) <div class= "col-md-10" > @Html. Editorf or (model => model. Description, new {htmlattributes = new {@class = "Form-control"}}) @Html. validationmessagefor (model => model. Description, "", new {@class = "Text-danger"}) </div> </div> <div class= "Form-group" > <d IV class= "Col-md-offset-2 col-md-10" > <input type= "Submit" value= "Save" class= "btn btn-default"/> </div&
Gt
</div> </div>} @section Scripts {@Scripts. Render ("~/bundles/jqueryval")}
3), add the submission of data received processing method
Post method for adding the Add method to the controller
[HttpPost]
[Validateantiforgerytoken]
Public ActionResult Add (role)
{
if (modelstate.isvalid)
{if (rolemanager.add)
. Code = = 1
{return
View ("Prompt", new Prompt () {Title = "Add role succeeded", Message
= "You have successfully added roles" + role. Name+ "" ",
Buttons = new list<string> () {" <a href=\ "" + url.action ("Index", "role") + "\ class=\" BTN btn-de Fault\ "> Role Management </a>", "<a href=\" "+ url.action (" Add "," role ") +" \ "class=\" btn btn-default\ "> Continue adding </a > "}}
);
}
return View (role);
}
5, the Administrator data modification
1), Add method
Add the Modify method to the controller.
<summary>
///Modify
///</summary>
///<param name= "id" >RoleID</param>
/// <returns></returns> public
actionresult Modify (int id)
{
var _role = rolemanager.find (ID);
if (_role = null) return View ("Prompt", New Prompt ()
{
Title = "error", message
= "id" "+ ID +" "Does not exist",
buttons = new list<string> () {"<a href=\" "+ url.action (" Index "," role ") +" \ "class=\" btn btn-default\ "> Character Tube </a> "}
});
Return View (_role);
}
2), add view
Right-click to add a view in a method
The code is as follows:
@model Ninesky.Core.Role @{viewbag.title = model.name;} @section sidenav{@Html. Partial ("Sidenavpartialview")} <ol class= "breadcrumb" > <li><span class= " Glyphicon glyphicon-home "></span> @Html. ActionLink (" homepage "," Index "," Home ") </li> <li>@
Html.ActionLink ("User Management", "index", "user") </li> <li> @Html. ActionLink ("Role Management", "Index", "roles") </li> <li class= "Active" > Modify </li> </ol> @using (Html.BeginForm ()) {@Html. AntiForgeryToken () <div Clas s= "Form-horizontal" > @Html. ValidationSummary (True, "", new {@class = "Text-danger"}) @Html. hiddenfor (Model => Model. Roleid) <div class= "Form-group" > @Html. Labelfor (model => model. Name, htmlattributes:new {@class = "Control-label col-md-2"}) <div class= "col-md-10" > @Html. Editorfor (Mode L => model. Name, new {htmlattributes = new {@class = "Form-control"}}) @Html. validationmessagefor (model => model. Name, "", new {@class = "Text-danger "}" </div> </div> <div class= "Form-group" > @Html. Labelfor (model => model. Description, htmlattributes:new {@class = "Control-label col-md-2"}) <div class= "col-md-10" > @Html. Editorf or (model => model. Description, new {htmlattributes = new {@class = "Form-control"}}) @Html. validationmessagefor (model => model. Description, "", new {@class = "Text-danger"}) </div> </div> <div class= "Form-group" > <d IV class= "Col-md-offset-2 col-md-10" > <input type= "Submit" value= "Save" class= "btn btn-default"/> </div&
Gt
</div> </div>} @section Scripts {@Scripts. Render ("~/bundles/jqueryval")}
3), add the submission of data received processing method
Add a post to the controller Modify method of the Submit processing method.
[HttpPost] [Validateantiforgerytoken] public actionresult Modify {if (modelstate.isvalid) {var _resp = Rol
Emanager.update (role); if (_resp. Code = = 1) return View ("Prompt", new Prompt () {Title = "Modify role succeeded", message = "You have successfully modified the role" "+ roles. Name + "" ", Buttons = new list<string> () {" <a href=\ "" + url.action ("Index", "role") + "\ class=\" BTN btn- Default\ "> Role Management </a>", "<a href=\" "+ Url.action" ("Modify", "roles", new {id = part). Roleid}) + "\ class=\" btn btn-default\ "> View </a>", "<a href=\" "+ url.action (" Add "," role ") +" \ class=\ "btn
Btn-default\ "> Add </a>"}}); else return View ("Prompt", new Prompt () {Title = "Modify role failed", message = "Reason for failure:" + _RESP. message, Buttons = new list<string> () {"<a href=\" "+ url.action (" Index "," role ") +" \ class=\ "BTN Btn-def Ault\ "> Role Management </a>", "<a href=\" "+ Url.action" ("Modify", "roles", new {id = part). Roleid}) + "\ class=\"BTN btn-default\ "> Return </a>"}});
else return to View (role);
}
6, delete the role
Add the Modify method to the controller.
<summary>
///Delete "Json"
///</summary>
///<param name= "id" >RoleID</param>
///<returns></returns>
[HttpPost]
Public Jsonresult Deletejson (int id)
{return
Json (Rolemanager.delete (ID));
}
Role complete, preview effect in F5 browser
---------------------------------------------------------------------------------------
Code See: Https://ninesky.codeplex.com/SourceControl/latest
Code Download: https://ninesky.codeplex.com Click Source code click Download to download the original file.
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.