View Model receiving Null values

Asked

Viewed 786 times

2

After a query in BD, my query returns some null values in some attributes and when passing the values to the model:

ViewModel.Participantes.Add(new RelParticipantesInscritosAtividadeVM { 
  RA = p.RA.Value, Nome = p.Nome, Modulo = p.Modulo.Value 
});

I get this error message:

The null object must have a value

How do I get my model to receive null values or replace null with a default value?

  • 2

    Only change your query to return a default value if it is null.

  • how to do this?

1 answer

1


it would be good if you made it clear which attributes cannot receive null, but anyway, depending on the type of attribute you can declare it as nullable something like:

public int? RA { get;set; }

or do a treatment to receive a value if the return of the bank is null:

RA = p.RA.Value == null ? "valor default" : p.RA.Value;

Browser other questions tagged

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