How to resolve error: The best overloaded method match for

Asked

Viewed 882 times

1

Following the models of that question, when I pass the commands to write the form data to the database, before even copying Visual Studio, points out the following error:

"The best overloaded method match for [...] has some invalid Arguments"

Follow the examples of the controller (remembering that the view and viewmodel are in the linked question at the beginning)

Controller

public ActionResult Novo()
    {
        ViewBag.Nacionalidade = new SelectList(db.Util.Where(u => u.tipo == 14), "id", "nome");

        var candidato = new CandidatoViewModel();

        return View(candidato);
    }

    [HttpPost]
    public ActionResult Novo(CandidatoViewModel candidato)
    {
        if (ModelState.IsValid) 
        {         
            db.Candidato.Add(candidato); // erro esta aqui

            db.SaveChanges();
            return RedirectToAction("VerCandidato", new { id = candidato.id });            
        }

        ViewBag.Nacionalidade = new SelectList(db.Util.Where(u => u.tipo == 14), "id", "nome", candidato.id_nacionalidade);

        return View(candidato);
    }
  • On which line does this error occur?

  • The error in question occurs when you pass invalid parameters when calling a method for example.

  • Ta ali no codigo Cigano, commented "db.Candidato.Add(candidate)/error esta aqui"

  • @Pedrocamarajunior But my Viewmodel and Model have exactly the same properties, except for the list I used in viewModel and the model has no... I still don’t understand very well how this mapping is done when you have multiple models, if you have articles/Docs for reference and studies would be of great help, without being those of codeproject...

1 answer

7


Well, it won’t work, obviously. You’re trying to add CandidatoViewModel in context, but a Viewmodel is not mapped by context by definition.

The right thing would be for you to do:

[HttpPost]
public ActionResult Novo(CandidatoViewModel candidato)
{
    if (ModelState.IsValid) 
    {         
        Candidato candidatoModel = candidato;
        db.Candidato.Add(candidatoModel); 

        db.SaveChanges();
        return RedirectToAction("VerCandidato", new { id = candidato.id });            
    }

    ViewBag.Nacionalidade = new SelectList(db.Util.Where(u => u.tipo == 14), "id", "nome", candidato.id_nacionalidade);

    return View(candidato);
}

This:

Candidato candidatoModel = candidato;

It’s worth it if you implement an implicit operator, as follows:

public class Candidato
{
    // Aqui vão as declarações normais do Model

    public static implicit operator Candidato(CandidatoViewModel c)
    {
        return new Candidato {
            Nome = c.Nome,
            Idade = c.Idade,
            ...
        };
    }
}

There are still those who use Automapper instead of using an implicit operator, but opinionated speaking, I’m not a fan.

  • I am obliged to explain the fields even though they are exactly the same names?

  • And because you’re not a fan of the Automapper?

  • No, you can use them with them if you like. Automapper I tried to use here and gave me problems, especially with 1 to N, which is exactly your case.

Browser other questions tagged

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