Getting Foreign Key value - Object reference not set to an instance of an object

1

Using Code First - MVC 4 - EF6 I have the following Model:

[Table("usuario")]
public class Usuario
{
    [Key]
    [Column("UsuarioId")]
    public int UsuarioId { get; set; }

    public string Nome { get; set; }
}

[Table("Orcamento")]
public class Orcamento
{
    public int OrcamentoId { get; set; }

    [ForeignKey("Usuario")]
    public int UsuarioId { get; set; }

    public virtual Usuario Usuario { get; set; }
}

On my controller, I get a Paged Budget list.

    [Authorize]
    public ActionResult Index(int? page)
    {
        OrcamentoService orcamentos = new OrcamentoService();
        IQueryable<Orcamento> o = orcamentos.ListarTodos().OrderBy(x => x.OrcamentoId);

        int paginaTamanho = 20;
        int paginaNumero = (page ?? 1);

        IPagedList<Orcamento> orcamento = o.ToPagedList(paginaNumero, paginaTamanho);

        return View(orcamento);
    }

And in my View, I display in a foreach

@using PagedList;
@using PagedList.Mvc;
@model PagedList.IPagedList<Squadra.Price.Models.Orcamento>
...
@foreach (var item in Model)
                    {
                        <tr> 
                            <td>@item.OrcamentoId</td>
                            <td>@item.Usuario.Nome</td>                                
                        </tr>
                    }

If the User ID is registered, it displays without any problem but if the user ID does not exist, I get the error "Object reference not set to an instance of an object." and the problem is in the navigation '@ item.User.Name'.

Is there a way to display this field if it does not find any ID with this user?

    
asked by anonymous 13.07.2014 / 04:07

2 answers

2

Your model is missing the relationship (1 user can have 1 or multiple budgets) and in the code to ensure use Include since your relationship with the question is mandatory, and the Include forces the relationship to load.

Changes

//////////////////////////////////////////////////////////////////////
[Table("usuario")]
public class Usuario
{
    public Pessoas()
    {
        this.Orcamentos = new HashSet<Orcamentos>();
    }       
    [Key]
    [Column("UsuarioId")]
    public int UsuarioId { get; set; }
    public string Nome { get; set; }    
    public virtual ICollection<Orcamento> Orcamentos { get; set; }
}
//////////////////////////////////////////////////////////////////////
[Table("Orcamento")]
public class Orcamento
{
    public int OrcamentoId { get; set; }

    [ForeignKey("Usuario")]
    public int UsuarioId { get; set; }
    public virtual Usuario Usuario { get; set; }
}
//////////////////////////////////////////////////////////////////////
[Authorize]
public ActionResult Index(int? page)
{
    OrcamentoService orcamentos = new OrcamentoService();
    IQueryable<Orcamento> o = orcamentos.ListarTodos()
                                        .Include(inc => inc.Usuario)
                                        .OrderBy(x => x.OrcamentoId);
    int paginaTamanho = 20;
    int paginaNumero = (page ?? 1);
    IPagedList<Orcamento> orcamento = o.ToPagedList(paginaNumero, paginaTamanho);
    return View(orcamento);
}
//////////////////////////////////////////////////////////////////////
    
14.07.2014 / 01:22
1

Switch:

<td>@item.Usuario.Nome</td>

By:

<td>@(item.Usuario != null ? item.Usuario.Nome : "")</td>
    
13.07.2014 / 09:13