Chapter 3 Add User Group Operation 3.2, mvc43.2
I. Users
Ii. User Group
2.1 browsing user groups
2.2 Add a user group
Modify [Add] Action
/// <Summary> /// Add User Group /// </summary> /// <returns> </returns> [AdminAuthorize] public ActionResult Add () {ViewData. add ("Type", TypeSelectList); return View ();}
Add a strong view
The Code is as follows:
@ Model Ninesky. Models. UserGroup @ {ViewBag. Title = "Add User Group"; Layout = "~ /Views/Layout/_ Manage. cshtml ";} <div class = "left"> <div class = "top"> </div> list on the left </div> <div class = "split"> </div> <div class = "workspace"> <div class = "inside"> <div class = "notebar"> Add a user group </div> @ using (Html. beginForm () {@ Html. validationSummary (true) <fieldset> <legend> User Group Information </legend> @ Html. hiddenFor (model => model. userGroupId) <ul> <li> <div class = "editor-label"> @ Html. labelFor (model => model. type) </div> <div class = "editor-field"> @ Html. dropDownList ("Type") </div> </li> <div class = "editor-label"> @ Html. labelFor (model => mod El. name) </div> <div class = "editor-field"> @ Html. editorFor (model => model. name) @ Html. validationMessageFor (model => model. name) </div> </li> <div class = "editor-label"> @ Html. labelFor (model => model. description) </div> <div class = "editor-field"> @ Html. editorFor (model => model. description) @ Html. validationMessageFor (model => model. description) </div> </li> <div class = "editor-label"> </div> <Div class = "editor-field"> <input type = "submit" value = "save"/> </div> </li> </ul> </fieldset>} </div> <div class = "clear"> </div> @ section Scripts {@ Scripts. render ("~ /Bundles/jqueryval ")}
Modify the User Group to Add the processing Action [Add (UserGroup userGroup)] Action, the code after completion
[HttpPost] [AdminAuthorize] public ActionResult Add (UserGroup userGroup) {userGroupRsy = new UserGroupRepository (); if (userGroupRsy. add (userGroup) {Notice _ n = new Notice {Title = "User Group added successfully", Details = "you have successfully added [" + userGroup. name + "] user group! ", DwellTime = 5, NavigationName =" User Group list ", NavigationUrl = Url. action ("List", "UserGroup")}; return RedirectToAction ("ManageNotice", "Prompt", _ n );} else {Error _ e = new Error {Title = "failed to add user group", Details = "failed to save to database when adding user group", Cause = "system Error ", solution = Server. urlEncode ("<li> Returns <a href = '" + Url. action ("Add", "UserGroup") + "'> Add User </a> page, enter the correct information and try again </li> <li> contact the website administrator </li> ")}; return RedirectToAction (" ManageError "," Prompt ", _ e );}}
Check in the browser
Enter the required information to test. You can add the required information.
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.