Before proceeding to this tutorial please go to Asp.Net MVC Membership Provider
In previous tutorial we learned how to use membership provider in asp.net mvc.Here we will learn how to Display Roles for the users in Asp.Net MVC Membership.
In previous tutorial we learned how to use membership provider in asp.net mvc.Here we will learn how to Display Roles for the users in Asp.Net MVC Membership.
First
proceeding to this we need to add a class or we need a model name is Role. Add this role model inside the account
model.
Add the
following code to the account model.cs class
[Table("webpages_Roles")]
public class Role
{
[Required(ErrorMessage = "Enter Role name")]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
public string RoleName { get; set; }
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int RoleId { get; set; }
}
|
After creating this Role model we need to add
a Acton method name RoleCreate for HttpPost and HttpGet , so for that write
following code in account controller
[HttpGet]
public ActionResult UserRole()
{
return View(new Role());
}
[HttpPost]
public ActionResult UserRole(Role role)
{
return View(role);
}
|
After
creating action method for Role we
need to add a view for this, For adding view right click on the “UserRole”
action method a window will open there you select model class as Role model and select scaffold template as Create,And after selecting all these click on
Add.
It will add
a view for you and you will see this view under the account folder in View.
Now run
your application and login to the page which you created account before. After
login to account ,navigate to the following URL
http://localhost:50526/Account/UserRole
|
When you will hit this URL it will go to
userRole view and page will look like this
After
making this view we need to insert role to database so for that we need to add
code to insert data into database , so for that we need to add code in HttpPost method of UserRole action method.
Inside this
action method there is a property name is [Roles.RoleExists(role.RoleName)
]to check that this
Role is already exist or not in database , it return Boolean value. So write the
following code in UserRole Action
method.
[HttpPost]
public ActionResult UserRole(Role role)
{
if (ModelState.IsValid)
{
if (Roles.RoleExists(role.RoleName))
{
ModelState.AddModelError("Error", " This Rolename
already exists");
return View(role);
}
else
{
Roles.CreateRole(role.RoleName);
return RedirectToAction("RoleIndex", "Account");
}
}
else
{
ModelState.AddModelError("Error", "Please enter proper
Username and Password");
}
return View(role);
}
|
So far we
created Role for the user. Now we will
learn how to assign role to the user.
Assign Roles to the user And Display
the Role
For
displaying all the user role we will use code first approach so for that we
have to creaet a class name is “userContext” which will be inherited from the “DbContext” class .If you are internet
application then is it already exist in account model at top. Code for this
class is like following
public class UsersRoleContext : DbContext
{
public UsersRoleContext()
: base("DBConnectionForMembership")
{
}
public DbSet<Role> UserRoles { get; set; }
}
|
DBConnectionForMembership is the connection string name which
we defined in web config file.
Now we need to add action method for displaying the role
of users and add a action methos in account controller class name is
“DisplayRoleForUsers”.
[HttpGet]
public ActionResult DisplayRoleForUsers()
{
IEnumerable<Role> ListRoles;
using (UsersRoleContext db = new UsersRoleContext())
{
ListRoles =
db.UserRoles.ToList();
}
return View(ListRoles);
}
|
After
adding action method ,now need to add view for displaying the role of user so
for that right click on DisplayRoleForUsers action method add a view without creating strongly typed view.
Now this
view will create a empty view and for showing role of user we need a grid.
Adding MVC Grid to application
For adding
grid to the application right click on the application and select “Nuget
package manager”
After click
on nugget packet manager a window will open
there you search “Grid.Mvc”
under online panel. And install this grid to the application.
After
adding grid to the application go the “DisplayRoleForUsers”
view
Add grid.mvc to this view
Write the
following code to this view
@model IEnumerable<MvcMembershipProvider.Models.Role>
@using GridMvc.Html
@{
ViewBag.Title = "Display roles for
users";
}
<h2>DisplayAllRoles</h2>
<link href="~/Content/Gridmvc.css" rel="stylesheet" />
<link href="~/bootstrap/css/bootstrap.min.css" rel="stylesheet" />
<script src="~/Scripts/jquery-1.9.1.min.js"></script>
<script src="~/Scripts/gridmvc.js"></script>
@Html.Grid(Model).Columns(columns
=>
{
columns.Add(c => c.RoleId).Titled("Role ID").SetWidth(300);
columns.Add(c => c.RoleName).Titled("RoleName").Filterable(true).SetWidth(300);
}).WithPaging(5).Sortable(true)
|
Now run you application and go to the followinf URL and see the output
http://localhost:50526/Account/DisplayRoleForUsers
|