Undefined object reference for an instance - Null foreign key

Asked

Viewed 30 times

1

Gentlemen(s),

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

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

2° - The secretary validates these documents including complementary information, including the employee email (userid) and making changes if necessary

It happens that when the receptionist makes the first registration of this document in the system, the field "Userid" is null, so that the secretary determines to whom each of these documents will go.

This is the view that is giving 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 Documento

 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; }
}

Employee Model

    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 let in the bank the "Userid" accept null I have tried checking the view itself if the object is null and show an empty field I have tried to include Try Catch Nullexception and show a message stating that the email field is required, but always... ALWAYS returns this error... I just don’t know what else to do.

Thank you to all who help me.

inserir a descrição da imagem aqui

1 answer

1

Try the following check on your View, I did a two-step check, first confirms if 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>
  • Thanks for the reply! Got it here

  • @Pedrobaraldini If that answer helped you, mark her as accepted. If you have achieved otherwise, post your answer and mark it as accepted. If you do not ask your question is open on the site ;)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.