Controller in MVC for the same Model but different role

1

Good morning, I am studying and I do not have much idea of how to do the following:

I have products as an entity. In the MVC model I have its Controller and its ProductViewModels. Well I have used this for the Admin, this create modifies and deletes products. But the question I have now is:

Customers see the products to buy them, how do I create new in index for these customers? I mean folder structure, in the same controller ...

I know I have to make a new model for these, so they see something less than the admin. But the question is the controller, since I used the Index to list the products to admin ... How do I do it to see the products for the user? You see products- > list with an image, then click and see the details (used by admin too) ....

I already have the product folder created in the view. Greetings

    
asked by Miguel-Ángel 20.07.2017 в 08:01
source

3 answers

1

Assuming that you use Identity for the management of users and roles there are also other ways to display content on a page depending on the user. I explain a simple way in which you can use the same Index for 2 roles.

Assuming that in your Index view of the controller x you render the body of the table, you can define which buttons can or can not be seen for the user logged in this way:

 <tbody>
    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Nombre)
            </td>

            <td>
                @Html.DisplayFor(modelItem => item.Apellidos)
            </td>

            <td>
                @Html.DisplayFor(modelItem => item.Cargo)
            </td>

            <td>
                @Html.DisplayFor(modelItem => item.Area)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Entidad)
            </td>
            <td>                    
                <a class="btn btn-info btn-sm view" data-toggle="tooltip" data-placement="top" title="Detalles" href="@Url.Action("Detalles", new {Id = item.Id})"><i class="glyphicon glyphicon-align-left"></i></a>

                  @if (User.IsInRole("ADMINISTRADOR"))
                      {
                         <a class="btn btn-warning btn-sm edit" data-toggle="tooltip" data-placement="top" title="Editar" href="@Url.Action("Editar", new {Id = item.Id})"><i class="glyphicon glyphicon-edit"></i></a>
                         <a class="btn btn-danger btn-sm eliminar" data-toggle="tooltip" data-placement="top" title="Eliminar" href="@Url.Action("Eliminar", new {Id = item.Id})"><i class="glyphicon glyphicon-trash"></i></a>
                        }

            </td>
        </tr>
    }
</tbody>

This way you are defining that all users who can access Index will see the Details button, but only users logged in as Administrator can see the Edit and Delete button. This you can use it in the view you want and to show what you want. Always remember that in the controller you would already have to specify the users in the [Authorize] annotation for each action. This is a simple way to reuse your views and to get out of trouble quickly, but in large and complex applications you should make use of areas as Sergio Parrra adds in his answer, so you should take a look. I hope it helps you

    
answered by 20.07.2017 в 17:30
0

in ASP.NET MVC there is the concept of Area . It is a feature used to organize related functionality such as a separate namespace or a different folder structure. You can create an area for the administrator and another for clients. Here is a post about what the Areas are and how it is implemented. Areas in ASP.NET MVC - A way to organize our applications

    
answered by 20.07.2017 в 09:29
0

Thanks for the help, in the end I made it with Areas, but I will try this other option too, I'm sure I have to use it when I implement the roles.

    
answered by 22.07.2017 в 10:35