Search code examples
c#asp.netrazor

Page visible only if user is selected


I want to show the user permissions page only if at first the user is selected. This is the code to select the user in cshtml:

<div class="p-a-1">
    <select required class="form-control" style="overflow:auto;" id="Username">
        <option id="tipoUtente" value="0">&lt;Selezionare un utente&gt;</option>
        @foreach (var utente in
    GestioneUtenti.Select(new WhereStatementBuilder { { GestioneUtenti.Empty.Ruolo, Operator.Equals, 2} }))
        {
            <option value="@utente.IdUser">@utente.Username</option>
        }
    </select>
</div>

and then I add the permissions:

<div class="p-a-1">
    <select required class="form-control" style="overflow:auto;" id="Username">
        <option id="tipoUtente" value="0">&lt;Selezionare un utente&gt;</option>
        @foreach (var utente in
    GestioneUtenti.Select(new WhereStatementBuilder { { GestioneUtenti.Empty.Ruolo, Operator.Equals, 2} }))
        {
            <option value="@utente.IdUser">@utente.Username</option>
        }
    </select>
</div>

What type of control I have to add to hide the page until the user is selected?


Solution

  • Since the project is a razor project, you can be serving the page through some method. For example,

    public ActionResult Index()
    {
        // some code
        return View();
    }
    

    You would also be willing to show something else if the permissions page is not visible. Add an if condition to the view to be displayed. Like this:

    public ActionResult Permissions()
    {
        if(userSelected())
            return View(); // shows permissions view
        return View("UserNotSelected"); // shows the view with some error message
    }