The main implementation of the management of the background interface user data modification and deletion, modify user data and role is often used in the function, but the deletion of the user situation is relatively small, in order to complete the function or sit on the. The main use is two action "Modify" and "Delete".
I. Modification of user data (Modify)
This feature is divided into two sections:
Public ActionResult Modify (int id) for displaying user information
[HttpPost]
Public ActionResult Modify (formcollection form) users receive information from the foreground and modify
1. Display user Information
<summary>
///Modify user information
///</summary>
///<param name= "id" > User primary key </param>
/ <returns> partial 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;
Role list End return
Partialview (Usermanager.find (ID));
}
This action has a parameter ID, receives the incoming user ID, queries the role information in the action, passes the viewbage to the view, and passes the return Partialview (Usermanager.find (ID)) Passing the user model to the view returns a partial 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. DropDownList For (model => model. Roleid, (ienumerable<selectlistitem>) Viewbag.roles, new {@class = "Form-control"}) @Html. Validationmessagefor ( Model => model. Roleid, "", new {@class = "Text-danger"}) </div> </div> <div class= "Form-group" > @Html. Labe Lfor (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 =&Gt Model. Username, "", new {@class = "Text-danger"}) </div> </div> <div class= "Form-group" > @Html. La Belfor (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. labelf or (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) Confidential @Html. VALIDATIONMESSAGEFOR (model => model. Sex, "", new {@class = "Text-danger"}) </div> </div> <div class= "Form-group" > @Html. Labelfo R (model => model. PasSword, htmlattributes:new {@class = "Control-label col-md-2"}) <div class= "col-md-10" > @Html. Editorfor (mod El => model. Password, new {htmlattributes = new {@class = "Form-control"}}) @Html. validationmessagefor (model => model. Password, "", new {@class = "Text-danger"}) </div> </div> <div class= "Form-group" > @Html. La Belfor (model => model. Email, htmlattributes:new {@class = "Control-label col-md-2"}) <div class= "col-md-10" > @Html. Editorfor (mod El => model. Email, new {htmlattributes = new {@class = "Form-control"}}) @Html. validationmessagefor (model => model. Email, "", new {@class = "Text-danger"}) </div> </div> <div class= "Form-group" > @Html. Label For (model => model. Lastlogintime, htmlattributes:new {@class = "Control-label col-md-2"}) <div class= "col-md-10" > @Html. Edito Rfor (model => model. Lastlogintime, new {htmlattributes = new {@class = "form-Control ", disabled =" Disabled "}) @Html. validationmessagefor (model => model. Lastlogintime, "", new {@class = "Text-danger"}) </div> </div> <div class= "Form-group" > @Ht ml. Labelfor (model => model. Lastloginip, htmlattributes:new {@class = "Control-label col-md-2"}) <div class= "col-md-10" > @Html. Editorf or (model => model. Lastloginip, new {htmlattributes = new {@class = "Form-control", disabled = "Disabled"}}) @Html. validationmessagef or (model => model. Lastloginip, "", new {@class = "Text-danger"}) </div> </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 (M Odel => model. Regtime, new {htmlattributes = new {@class = "Form-control", disabled = "Disabled"}}) @Html. Validationmessagefor (M Odel => model.
Regtime, "", new {@class = "Text-danger"})</div> </div> </div>}
2, modify the background of user data processing
[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)
{
_r Esp. Code = 0;
_resp. Message = "User does not exist, may have been deleted, refresh and retry";
}
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 parameter IDs and formcollection form, the reason for not using user to do the model directly is because user will receive all the data from the foreground, I do not want to allow to modify the username. So using the TryUpdateModel binding in the method allows the user to modify the properties. TryUpdateModel will also log errors in the modelstate when the binding fails, and you can use the custom method getmodelerrorstring get the error message and feed back to the view.
2, the foreground display and processing
Open the index view to find the table initialization method, format the column "Username" so that it displays a connection, the Code Red section.
Make it look like this, when the user clicks on the connection can display the Change dialog box
Pop-up windows 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: "<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: "Glyphico N 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", actio N:function (dialogitself) {$table. bootstraptable ("refresh");
Dialogitself.close ();
Modifydialog.close ();
}
}]
}); else Bootstrapdialog.alert (data.
message);
}, "JSON");
$ ("form"). Validate ();
}, {icon: "Glyphicon glyphicon-remove", Label: "Off", Action:function (dialogitself) {
Dialogitself.close ();
}
}]
});
Modifydialog.open ();
});
Modify End}
The display effect is as follows figure
Second, delete users
Add Delete method in Usercontroller
<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 to find the table initialization method, add the Action column format column so that it displays a delete button, the Code Red Box section.
Foreground display effect
Then in the form of the Onloadsuccess method just write to modify the user information of the JS code behind the delete user's JS code
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 recommend that you 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
: "Delete user succeeded",
buttons: [{
icon: "Glyphicon Glyphicon-ok", C12/>label: "OK",
action:function (dialogitself) {
$table. bootstraptable ("refresh");
Dialogitself.close ();
}
}]
}
else Bootstrapdialog.alert (data. message);
}, "json");
}
});
Delete button End
}
});
End of table
Foreground display effect
==========================================
Code download See http://www.cnblogs.com/mzwhj/p/5729848.html
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.