Object reference not set to an instance - Foreign Key Null

1

Gentlemen,

I have a relationship between two tables, Employee and Documents (1: N)

There are two steps in my system: 1 ° - The receptionist registers with basic information a document that arrives at the institution and sends an email to the responsible person to make the withdrawal of these.

2 ° - The secretary validates these documents including additional information, including the employee's email (userId) and making changes if necessary

It turns out that when the receptionist does the first registration of this document in the system, the "UserId" field is null, so that the secretary determines to whom each of these documents goes.

This is the view that is causing problems:

<table class="table table-bordered data-table" style="font-size: 12px">
    <tr>
        <th>Remetente</th>
        <th>@Html.DisplayNameFor(m => m.DocumentoTipoId)</th>
        <th>@Html.DisplayNameFor(m => m.DataRecebimentoRecepcao)</th>
        <th>@Html.DisplayNameFor(m => m.DataRecebimentoSecretaria)</th>
        <th>@Html.DisplayNameFor(m => m.Observacao)</th>
        <th>@Html.DisplayNameFor(m => m.UserId)</th>
        <th>Ação</th>
    </tr>

    @foreach (var docs in Model.Documentos) {
        var dataRec = docs.DataRecebimentoRecepcao.HasValue ? docs.DataRecebimentoRecepcao.Value.ToString() : "";
        var dataSec = docs.DataRecebimentoSecretaria.HasValue ? docs.DataRecebimentoSecretaria.Value.ToString() : "";
        <tr>
            <td>@docs.Remetente</td>
            <td>@docs.DocumentoTipo.Tipo</td>
            <td>@dataRec</td>
            <td>@dataSec</td>
            <td>@docs.Observacao</td>
            <td>@docs.UserProfileV.UserName</td> @*O ERRO ESTÁ NESTA LINHA*@

            <td>
                <button type="button" class="btn btn-primary" title="Editar Documento"
                        onclick="location.href = '@Url.Action("ValidarDocumento", new { id = MyCrypto.Encrypt(docs.DocumentoId.ToString()) })'">
                    <span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>
                </button>
            </td>
        </tr>
    }
</table>

Model Document

 public class Documento {

    public int DocumentoId { get; set; }
    public string Remetente { get; set; }
    public DateTime? DataRecebimentoRecepcao { get; set; }
    public DateTime? DataRecebimentoSecretaria { get; set; }
    public bool Retirado { get; set; }
    public string Observacao { get; set; }

    // Relacionamento 1:N

    public int DocumentoTipoId { get; set; }
    public virtual DocumentoTipo DocumentoTipo { get; set; }

    public int UserId { get; set; }
    public virtual UserProfileContext UserProfileV { get; set; }
}

Model Employee

    public class UserProfileContext {
    public UserProfileContext() {
        this.DadosComplementares = new List<DadosComplementares>();
        this.webpages_Roles = new List<webpages_Roles>();
        this.Chamados = new List<ChamadoContext>();
        this.Interessados = new List<InteressadosContext>();
        this.Tecnicos = new List<TecnicoContext>();
        this.Documentos = new List<Documento>();
        //this.DocumentosFuncionarios = new List<DocumentoFuncionario>();

    }

    public int UserId { get; set; }
    public string UserName { get; set; }
    public virtual ICollection<ChamadoContext> Chamados { get; set; }
    public virtual ICollection<ChamadosMkt> ChamadosMkt { get; set; }
    public virtual ICollection<DadosComplementares> DadosComplementares { get; set; }
    public virtual ICollection<webpages_Roles> webpages_Roles { get; set; }
    public virtual ICollection<InteressadosContext> Interessados { get; set; }
    public virtual ICollection<TecnicoContext> Tecnicos { get; set; }
    public virtual ICollection<TecnicoMkt> TecnicosMkt { get; set; }
    public virtual ICollection<InteressadosMkt> InteressadosMkt { get; set; }
    public virtual ICollection<Gestores> Gestores { get; set; }
    public virtual ICollection<ControleChaves> ControleChaves { get; set; }
    public virtual ICollection<Documento> Documentos { get; set; }
}

I already tried to leave in the bank the "UserId" accept null I have already tried to make checks in the view itself if the object is null and show an empty field I've tried to include Try Catch NullException and display a message stating that the email field is mandatory, but always ... ALWAYS returns this error ... I just do not know what to do.

Thank you all for helping me.

    
asked by anonymous 10.05.2018 / 14:02

1 answer

1

Try the following check in your View , did a 2-step check, first confirm that the object is null and then UserName

<table class="table table-bordered data-table" style="font-size: 12px">
    <tr>
        <th>Remetente</th>
        <th>@Html.DisplayNameFor(m => m.DocumentoTipoId)</th>
        <th>@Html.DisplayNameFor(m => m.DataRecebimentoRecepcao)</th>
        <th>@Html.DisplayNameFor(m => m.DataRecebimentoSecretaria)</th>
        <th>@Html.DisplayNameFor(m => m.Observacao)</th>
        <th>@Html.DisplayNameFor(m => m.UserId)</th>
        <th>Ação</th>
    </tr>

    @foreach (var docs in Model.Documentos) {
        var dataRec = docs.DataRecebimentoRecepcao.HasValue ? docs.DataRecebimentoRecepcao.Value.ToString() : "";
        var dataSec = docs.DataRecebimentoSecretaria.HasValue ? docs.DataRecebimentoSecretaria.Value.ToString() : "";
        <tr>
            <td>@docs.Remetente</td>
            <td>@docs.DocumentoTipo.Tipo</td>
            <td>@dataRec</td>
            <td>@dataSec</td>
            <td>@docs.Observacao</td>
            <td>
                @if(docs.UserProfileV != null && docs.UserProfileV.UserName!= null)
                {
                    <label>
                        @docs.UserProfileV.UserName
                    </label>
                }
            </td> 

            <td>
                <button type="button" class="btn btn-primary" title="Editar Documento"
                        onclick="location.href = '@Url.Action("ValidarDocumento", new { id = MyCrypto.Encrypt(docs.DocumentoId.ToString()) })'">
                    <span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>
                </button>
            </td>
        </tr>
    }
</table>
    
10.05.2018 / 14:51