How to approve a model depending on another model confirmation?

Asked

Viewed 72 times

1

How do I make for the View request only appear to the user, when the class confirmation Aprovacao for true.

Follows the logic:

1- user creates the request and register,

2- user awaits confirmation of the request made

3- if the request is approved

4- user can see the registered request

Models:

public class Solicitacao
{
    public int SolicitacaoId { get; set; }

    public bool Status { get; set; }


    public string servico { get; set; }
}

I would like this class to confirm the request

public class Aprovacao
{
    public  int AprovacaoId  { get; set; }


    public string Aprova { get; set; }

   public int SolicitacaoId { get; set; }

   public virtual Solicitacao _Solicitacao{ get; set; }

}

View:

@using (Html.BeginForm()) 
 {
@Html.AntiForgeryToken()

<div class="form-horizontal">
    <h4>Solicitacao</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group">
        @Html.LabelFor(model => model.Status, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            <div class="checkbox">
                @Html.EditorFor(model => model.Status)
                @Html.ValidationMessageFor(model => model.Status, "", new { @class = "text-danger" })
            </div>
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Servico, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Servico, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Servico, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Create" class="btn btn-default" />
        </div>
    </div>
</div>
}

What method would I use to make this confirmation in controller approval?

  • Welcome Elói Ferreira. Also enter the code of your view. When possible do the [Tour] site to better understand the community.

  • Thanks, @Georgewurthmann! I’ve added to View

  • @Elóiferreira But at what point does this approval have to be made, it has to be approved by another system user? This flow is not very clear.

  • @Edneybatistadasilva In my case the user makes the request and Adm approves. I saw in some places that the application would have to persist in the bank anyway, right? And then I want the request to appear saved on the user screen, only when the Adm do is approved, before that when the request registration persists, a message appeared to the user to wait for the Adm approval.

1 answer

0

By saving your request you can redirect the user to the screen with the lists of all requests:

    [HttpPost]
    public ActionResult Create(Solicitacao solicitacao)
    {
        SeuRepositorio.SalvarSolicitacao(solicitacao);
        return RedirectToAction("Index");
    }

In the view index you can list all open requests on the system, and show a message to the user that there are requests that have not yet been approved.

These requests can be displayed in an ordered table, displaying the unapproved records at the beginning, or even the unapproved record lines can be highlighted in red.

So in your Index action it would look like this:

    public ActionResult Index()
    {
        List<Solicitacao> solicitacoes = SeuRepositorio.ObterTodasAsSolicitacoes();
        return View(solicitacoes);
    }

I am assuming that the status field of the request is what indicates that it is approved or not.

Now in your View Index you create your table, I recommend the use of bootstrap to make it easy to highlight lines:

@model List<AprovadorDeSolicitacoes.Models.Solicitacao>
<table class="table table-hover table-striped">
<thead>
    <tr>
        <th>Id</th>
        <th>Solicitacao</th>
        <th>#</th>
    </tr>
</thead>
<tbody>
    @foreach(var item in this.Model)
    {
        <tr class="@(item.Status ? "success" : "danger")">
            <td>@item.SolicitacaoId </td>
            <td>@item.servico  </td>
            <td>
                <a href="/Solicitacao/Detalhes/@item.SolicitacaoId">detalhes</a>
            </td>
        </tr>
    }
</tbody>
</table>

In turn on the details link where you redirect to the detail action you can do another validation and redirect the user to the appropriate view:

    public ActionResult Detalhes(int id)
    {
        var solicitacao = SeuRepositorio.AbrirSolicitacao(id);
        if(!solicitacao.Status)
            return View("ViewComMensagemDeNaoAprovado", solicitacao);
        return View(solicitacao); // manda para sua view que exibe os detalhes da solicitação já aprovada.
    }

I hope I’ve helped.

  • Thanks @Edneybatistadasilva, I will try to make your solution. Thank you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.