2
Within C# MVC5, I am creating a user registration form with several fields. One of them is the Function (Bonus) field, which the user may or may not have.
Follows the User class:
public class Usuarios
{
public int Id { get; set; }
[Required, StringLength(60)]
public string Nome { get; set; }
[DisplayName("Matrícula"),StringLength(7)]
public string Matricula {get;set;}
[DisplayName("DV"),Required]
public int MatriculaDV {get;set;}
[StringLength(20)]
public string OAB {get;set;}
[DisplayName("Gênero")]
public Genero Genero { get; set; }
public bool Ativo {get;set;}
[DisplayName("Data de Nascimento"), DisplayFormat(DataFormatString="{0:d}")]
public DateTime DataNascimento { get; set; }
[DisplayName("Unidade")]
public virtual int? UnidadesID {get;set;}
public virtual Unidades Unidade {get;set;}
[DisplayName("Cargo")]
public virtual int? CargosID { get; set; }
public virtual Cargos Cargo { get; set; }
[DisplayName("Função")]
public virtual int? FuncoesID {get;set;} //eis a funcao
public virtual Funcoes Funcao {get;set;}
}
And the class Functions:
public class Funcoes
{
public int Id { get; set; }
public int Codigo { get; set; }
[StringLength(70)]
public string Funcao { get; set; }
public virtual IList<Usuarios> Usuario { get; set; }
}
The inclusion form allows adding a new record with no function.
However, when trying to list them, the view containing the table generates the error below on the line where it tries to display a null function record (line @usuario.Funcao.Function):
An Exception of type 'System.Nullreferenceexception' occurred in App_web_tneo00cv.dll but was not handled in user code
Additional information: Object reference not defined for a instance of an object.
Follow the User Index view:
@model IList<Aplicacoes.Entidades.Usuarios>
@{
ViewBag.Title = "Usuários";
}
<h4>Lista de Usuários</h4>
<a class="btn btn-primary" href="Usuario/Form">Novo Usuário</a>
<a class="btn btn-default" href="Unidades/Index">Unidades</a>
<a class="btn btn-default" href="Cargo/Index">Cargos</a>
<a class="btn btn-default" href="Funcao/Index">Funções</a>
@*@Html.ActionLink("Novo Usuário", "Form", "Usuario", new { @class="btn btn-primary"})
@Html.ActionLink("Unidades", "Index", "Unidades", new { @class = "btn btn-default" })*@
<table class="table table-striped table-hover">
<thead>
<tr>
<th>Matricula</th>
<th>DV</th>
<th>Nome</th>
<th>Cargo</th>
<th>Função</th>
<th>Unidade</th>
<th>OAB</th>
<th>Data Nasc.</th>
</tr>
</thead>
<tbody>
@foreach (var usuario in @Model.OrderBy(usuario => usuario.Nome))
{
<tr>
<td>@usuario.Matricula.ToUpper()</td>
<td>@usuario.MatriculaDV</td>
<td>@usuario.Nome.ToUpper()</td>
<td>@usuario.Cargo.Cargo</td>
<td>@usuario.Funcao.Funcao</td> //o erro ocorre aqui
<td>@usuario.Unidade.Sigla</td>
<td>@usuario.OAB</td>
<td>@usuario.DataNascimento.ToShortDateString()</td>
</tr>
}
</tbody>
</table>
I tried to get around that way in the View itself, but it didn’t work:
if(@usuario.Funcao == null)
{
<td>-</td>
}else{
<td>@usuario.Funcao.Funcao</td> //o erro acontece aqui novamente
}
Follow the User Controller to verify:
public class UsuarioController : Controller
{
// GET: Usuario
private UsuarioDAO uDao;
private CargosDAO cDao;
private FuncoesDAO fDao;
private UnidadesDAO udDao;
public UsuarioController(UsuarioDAO uDao, CargosDAO cDao, FuncoesDAO fDao, UnidadesDAO udDao)
{
this.uDao = uDao;
this.cDao = cDao;
this.fDao = fDao;
this.udDao = udDao;
}
public ActionResult Index()
{
IList<Usuarios> usuarios = uDao.Lista();
return View(usuarios);
}
public ActionResult Form()
{
ViewBag.Cargos = cDao.Lista().OrderBy(Cargos =>Cargos.Cargo);
ViewBag.Funcoes = fDao.Lista().OrderBy(Funcoes => Funcoes.Funcao);
ViewBag.Unidades = udDao.Lista().OrderBy(Unidades => Unidades.Nome);
return View();
}
public ActionResult Adiciona(Usuarios usuario)
{
uDao.Adiciona(usuario);
return RedirectToAction("Index");
}
public ActionResult Remove(int id)
{
Usuarios usuario = uDao.BuscaPorId(id);
uDao.Remove(usuario);
return RedirectToAction("Index");
}
public ActionResult Detalhes(int id)
{
Usuarios usuario = uDao.BuscaPorId(id);
return View(usuario);
}
public ActionResult Atualiza(Usuarios usuario)
{
uDao.Atualiza(usuario);
return RedirectToAction("Index");
}
}
I understand the error prompts me to initiate the variable first. But I can’t imagine where I can start this variable, since it’s a "class within a class".
Guys, I’m still in need of help to overcome this problem. Thank you :)
– romalito
Have you checked if all users on the list have Functions? In your view try something like this:
<td>@usuario.Funcao?.Funcao</td>
– Thiago Lunardi
Where’s your list method ()
– Marco Souza