8
I am refactoring a project in ASP.NET MVC 5, implementing some good practices and researching, it was mentioned that the use of if na view(Razor) is not the best practice, only that I use it to show or hide some components(fields) according to the current user’s permission, I am searching how to limit this by the controller so that the view rendering is performed, but I am not able to find a way to do this (I don’t even know if it is possible really), I will put an example of a current view code:
@if ((bool)Session["TipoChamadoVisivel"])
{
<div class="form-group">
@Html.LabelFor(model => model.TipoChamado, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.RadioButtonFor(model => model.TipoChamado, "1")<label> Totvs RM</label>
<label> | </label>
@Html.RadioButtonFor(model => model.TipoChamado, "2")<label> Outros</label>
<p>@Html.ValidationMessageFor(model => model.TipoChamado, "", new { @class = "text-danger" })</p>
</div>
</div>
}
@if ((bool)Session["ObraVisivel"])
{
<div class="form-group">
@Html.Label("Obra Destino", new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.DropDownListFor(m => m.ObraDestino, new SelectList(new prj_chamadosBRA.Repositories.ObraDAO().BuscarObrasPorUsuario(ViewBag.UserId), "IDO", "Descricao"), "-- Selecione a Obra --", new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.ObraDestino, "", new { @class = "text-danger" })
</div>
</div>
}
@if ((bool)Session["SetorVisivel"])
{
if (Session["PerfilUsuario"].ToString() != "1")
{
<div class="form-group">
@Html.Label("Setor Destino", new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.DropDownListFor(m => m.SetorDestino, new SelectList(new prj_chamadosBRA.Repositories.SetorDAO().BuscarSetoresPorObra(ViewBag.SetorDestino), "Id", "Nome"), new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.SetorDestino, "", new { @class = "text-danger validacaoSetor" })
</div>
</div>
}
else
{
<div class="form-group">
@Html.Label("Setor Destino", new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.DropDownListFor(m => m.SetorDestino, new SelectList(string.Empty, "Id", "Nome"), "-- Selecione o Setor --", new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.SetorDestino, "", new { @class = "text-danger" })
</div>
</div>
}
}
@if ((bool)Session["SelecionarResponsavelAbertura"])
{
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" data-parent="#accordion" href="#pnlResponsavelAbertura">Abrir chamado em nome de outro usuário?</a>
</h4>
</div>
<div id="pnlResponsavelAbertura" class="panel-collapse collapse">
<div class="panel-body">
@if (Session["PerfilUsuario"].ToString() != "1" && Session["PerfilUsuario"].ToString() != "4")
{
<div class="form-group">
@Html.Label("Selecione Usuario:", new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.DropDownListFor(m => m.ResponsavelAberturaChamado, new SelectList(new prj_chamadosBRA.Repositories.ApplicationUserDAO().retornarUsuariosObra(ViewBag.SetorDestino), "Id", "Nome"), "-- Selecione o usuário --", new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.ResponsavelAberturaChamado, "", new { @class = "text-danger" })
</div>
</div>
}
else
{
<div class="form-group">
@Html.Label("Selecione Usuario:", new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.DropDownListFor(m => m.ResponsavelAberturaChamado, new SelectList(string.Empty, "Id", "Nome"), "-- Selecione a obra primeiro --", new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.ResponsavelAberturaChamado, "", new { @class = "text-danger" })
</div>
</div>
}
</div>
</div>
</div>
}