2

I have 2 types of roles for users eg: User & admin. I want to show Update link when login role is user , but when role is Admin n update and delete both link should appear in view Module.

how can I achieve this ? here is my code of view. in which I want to put both link for update and delete based on user role.

@model List<WebApplication5.Models.MyModel>

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Show</title>
</head>
<body style="background-color:aliceblue">

    <div>
       @TempData["Message"]

        <b><center>Data Of Table</center></b>
        <table style="background-color:azure" border="1" align="center" title="DATA OF EMPLOYEE">

                @foreach (var objmymodel in Model)
                { 
                    <tr>
                       <td>@objmymodel.Username</td>
                       <td>@objmymodel.Password</td>  

                       <td>@Html.ActionLink("delete", "Delete", new {id=@objmymodel.Id })</td>
                       <td>@Html.ActionLink("update", "Update", new { id = @objmymodel.Id })</td>


                    </tr>
                }
        </table>
    </div>


    <div>@Html.ActionLink("Logout","Login","Mycontroller")</div>
</body>
</html>

here is code of ADO class if i enter admin as user name and password than redirect to show data page as admin or check in data base for username

        public static void Logindata(MyModel objmymodel)
        {
            SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=c:\users\nisarg parekh\documents\visual studio 2013\Projects\WebApplication5\WebApplication5\App_Data\Database1.mdf;Integrated Security=True;MultipleActiveResultSets=True;Application Name=EntityFramework");
            con.Open();

            if(objmymodel.Username=="admin" && objmymodel.Password=="admin")
            {

                usernamestring = "admin";
                message = "welcome    " + objmymodel.Username;

            }
            else
            {
            string check = "select Username,Password from tb1 where Username='"+objmymodel.Username+"' and Password='"+objmymodel.Password+"'";
            SqlCommand cmd = new SqlCommand(check,con);
            SqlDataReader dr = cmd.ExecuteReader();



            if (dr.Read())
            {

                usernamestring = objmymodel.Username;
                message ="welcome    "+ objmymodel.Username;
            }
            else
            {
                message = "FAIL";
            }
            }

here is code of Controller

 public ActionResult Login()
        {


            return View();
        }

        [HttpPost]
        public ActionResult Login(MyModel objmymodel)
        {


            ado.Logindata(objmymodel);

            TempData["Message"] = ado.message;

            if (ado.message == "FAIL")
            {
                objmymodel.Username = "";
                objmymodel.Password = "";
                return RedirectToAction("Login");

            }
            else
            {
                TempData["Message"] = ado.usernamestring;

                return RedirectToAction("Show");
            }

        }
Lucifer
  • 1,594
  • 2
  • 18
  • 32
nisarg parekh
  • 413
  • 4
  • 23

2 Answers2

1

In this case you have to follow below task.

-First Approach

  1. Create Role for your Application. Creating Roles in Asp.net Identity MVC 5

  2. On Registration assign Role to your user. How to assign a Role to a user in MVC5?

  3. Now based on role you can disable/enable Links. Hide link based on Role

-Second Approach

Create two views of current show view (all the remaining data except these links remains same)

 a. With Update and Delete Link
 b. only With Update Link

In controller, put check on user name, if user is admin then render first view(a) else render second view (b).

Sandeep Rasgotra
  • 582
  • 1
  • 7
  • 18
0

You can have a property (HasAdminAccess) in Model, and use this to check on view and render buttons accordingly.

Model:

public class MyModel
{
   // Your other props.

   public bool HasAdminAccess { get; set; }
}

Controller/ Action:

if (objmymodel.Username == "admin" && objmymodel.Password == "admin") {

         usernamestring = "admin";
         message = "welcome    " + objmymodel.Username;
         objmymodel.HasAdminAccess = true;
 }

View:

@if (Model.HasAdminAccess)
{
    <td>@Html.ActionLink("delete", "Delete", new {id = @objmymodel.Id})</td>
}
<td>@Html.ActionLink("update", "Update", new { id = @objmymodel.Id })</td>

*

When you first request(GET) HasAdminAccess, will have false and delete button would not be rendered.