0
I’m trying to rescue data from a screen with a Id specific, to later record on another screen, but an error occurs saying:
"Undefined object reference for an object instance."
[HttpGet]
public ActionResult Registro(int id)
{
var model = new CombustivelViewModelRegistro();
try
{
var rep = new AtribuirVeiculoRepsitorio();
AtribuirVeiculo av = rep.ObterPorId(id);
model.Veiculo = av.Veiculo.Matricula;
model.Funcionario = av.Funcionario.Nome;
}
catch(Exception e)
{
ViewBag.Mensagem = e.Message;
}
return View(model);
}
[HttpPost]
public ActionResult Registro(CombustivelViewModelRegistro model)
{
if (ModelState.IsValid)
{
var c = new Combustivel();
try
{
c.DataAbastecimento = model.DataAbastecimento;
c.DataCadastro = DateTime.Now;
c.Kilomtragem = Convert.ToDouble(model.Kilometragem);
c.Quantidade = model.Quantidade;
c.IdCombustivel = model.IdCombustivel;
c.Veiculo.Matricula = model.Veiculo;
c.Funcionario.Nome = model.Funcionario;
var service = new CombustivelRepositorio();
service.Inserir(c);
return RedirectToAction("Index");
}
catch (Exception e)
{
ViewBag.Mensagem = e.Message;
}
}
return View();
}
On what line does it happen? Probably these calls to your repository that searches for a value may be returning a null, and you’re trying to assign a value to a property.
– Gabriel Coletta
Provide more details in your question as a question with the similar title has already been answered and given as closed
– Fabricio
Your Vehicle is null and you try to assign a value to its License Plate.
– Gabriel Coletta
I must initialize c.Vehicle = new Vehicle(); c.Employee = new Employee();
– MAXF Xavier
@Maxfxavier You are still receiving the error "Object reference not defined for an instance of an object"?
– Jéf Bueno