Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to browse user groups on the MVC4 production website

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article shows you how to develop browsing user group operations on the MVC4 production website. The content is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

I. users

II. User groups

2.1 browse user groups

Before you start browsing user groups, you should first consider the issue of permissions. Browsing, adding, modifying, and deleting user groups must be performed by system administrators, and the Action must verify that they are administrators, so add an AdminAuthorize. Right-click on the Extensions folder to add the class "AdminAuthorizeAttribute", which inherits from AuthorizeAttribute.

Rewrite AuthorizeCore (HttpContextBase httpContext), which returns true without writing any code.

Because the function of the administrator has not been done yet, the purpose is to add, delete, browse, and verify the rights of the administrator without verifying the administrator.

Using System;namespace System.Web.Mvc {/ Administrator Rights Verification / public class AdminAuthorizeAttribute:AuthorizeAttribute {protected override bool AuthorizeCore (HttpContextBase httpContext) {return true;}

Modify the [List] Action to add administrator rights verification.

/ user group list / user group type / [AdminAuthorize] public ActionResult List (int Id =-1) {userGroupRsy = new UserGroupRepository (); IQueryable _ userGroup; if (Id = =-1) _ userGroup = userGroupRsy.List (); else _ userGroup = userGroupRsy.List (Id); return View (_ userGroup);}

Id is a user group type, because the user group type is an enumeration type, starting from 0, so the browsing address is set to-1 without the id parameter to display all user groups, and when the number of id parameters is equal to the number of user groups of the specified type.

Right-click to strengthen the type "UserGroup" view List.cshtml to modify the generated code.

@ model IEnumerable@ {ViewBag.Title = "list of user groups"; Layout = "~ / Views/Layout/_Manage.cshtml";} list on the left

List of user groups @ Html.ActionLink ("add user groups", "Add" "UserGroup") @ Html.DisplayNameFor (model = > model.Name) @ Html.DisplayNameFor (model = > model.Type) @ Html.DisplayNameFor (model = > model.Description) @ foreach (var item in Model) {@ Html.DisplayFor (modelItem = > item.Name) @ Html.DisplayFor (modelItem = > item.Type) @ Html.DisplayFor (modelItem = > item.Description) @ Html.ActionLink ("modify", "Edit" New {id = item.UserGroupId}) | @ Html.ActionLink ("delete", "Delete", new {id = item.UserGroupId})}

Run the browser to see the effect, it's okay.

Now it's time to add a drop-down menu where you can select different user group types to display the corresponding type of user group.

Add attribute TypeSelectList in [UserGroupController]

/ SelectList list of user group types / public List TypeSelectList {get {List _ items = new List (); _ items.Add (new SelectListItem {Text = UserGroupType.Anonymous.ToString (), Value = ((int) UserGroupType.Anonymous) .ToString ()}); _ items.Add (new SelectListItem {Text = UserGroupType.Limited.ToString (), Value = ((int) UserGroupType.Limited) .ToString ()}) _ items.Add (new SelectListItem {Text = UserGroupType.Normal.ToString (), Value = ((int) UserGroupType.Normal) .ToString ()); _ items.Add (new SelectListItem {Text = UserGroupType.Special.ToString (), Value = ((int) UserGroupType.Special) .ToString ()}); return _ items;}}

Modify [List] Action code

/ user group list / user group type / [AdminAuthorize] public ActionResult List (int Id =-1) {userGroupRsy = new UserGroupRepository (); IQueryable _ userGroup; if (Id = =-1) _ userGroup = userGroupRsy.List (); else _ userGroup = userGroupRsy.List (Id); var _ typeLists = TypeSelectList; _ typeLists.Insert (0, new SelectListItem {Text = "all", Value = "- 1"}) If (_ typeLists.Any (t = > t.Value = = Id.ToString () _ typeLists.SingleOrDefault (t = > t.Value = = Id.ToString ()). Selected = true; ViewData.Add ("GroupTypeList", _ typeLists); return View (_ userGroup);}

Add after @ Html.ActionLink ("add user group", "Add", "UserGroup") in the L.cshtml view

User group type: @ Html.DropDownList ("GroupTypeList")

Bottom add

$("# GroupTypeList") .change (function () {_ window.location.href = "/ UserGroup/List/" + $(this) .children ("option:selected") .val ();})

The completed List.cshtml code is as follows:

@ model IEnumerable@ {ViewBag.Title = "list of user groups"; Layout = "~ / Views/Layout/_Manage.cshtml";} list on the left

List of user groups @ Html.ActionLink ("add user groups", "Add" "UserGroup") user group type: @ Html.DropDownList ("GroupTypeList") @ Html.DisplayNameFor (model = > model.Name) @ Html.DisplayNameFor (model = > model.Type) @ Html.DisplayNameFor (model = > model.Description) @ foreach (var item in Model) {@ Html.DisplayFor (modelItem = > item.Name) @ Html.DisplayFor (modelItem = > item.Type) @ Html.DisplayFor (modelItem = > item.Description) @ Html.ActionLink ("modify" "Edit", new {id = item.UserGroupId}) | @ Html.ActionLink ("delete", "Delete", new {id = item.UserGroupId})} $("# GroupTypeList") .change (function () {_ window.location.href = "/ UserGroup/List/" + $(this) .children ("option:selected") .val () })

Done, check it in the browser

The above content is the MVC4 production website in how to develop browsing user group operation, have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, 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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report