I am refactoring a project in ASP.NET MVC 5, implementing some good practices and researching, it was mentioned that the use of if in the view (razor) is not the best practice, but I use it to show or hide some components (fields) according to the current user's permission, I am researching how to limit this by the controller so that the rendering of the view is performed, but I can not find a way to do that (I do not even know if it's really possible), I'm going put an example of a code from the current view:
@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>
}