Well, I'm trying to implement the action of editing the data of a viewmodel I have in my project.
I even created an action, but it is not working ... What happens is that from what I researched, when you are going to build an edit action, you need to reference the Model Id so that it loads the data ... What happens is how ViewModel is, I do not map it to the database, so that way I can not get the Id.
I have several tables in my project, as I say in my question , where, to reinforce, I have several models that are in ViewMOdel.
That is, I have the CliClient, Table2, Table3, Table4, and Table5 models. Where CliClient relates to Table2. Table 2 is related to the others (Table 3, 4 and 5). Or, to be clearer, Tables 3, 4 and 5 relate to Table 2.
The part of the data entry in the system is already working, but now I need to edit .... The way I'm trying is, show the client name and from it, take all the other data. That is, I wanted to show the client's name and when the user clicks edit the data, the information from the other tables is loaded so that the user can edit the data.
I have these codes:
// GET: Anaminese/Edit/5
public ActionResult Edit(int? id)
{
CliCliente cliente = db.CliCliente.Find(id);
if (cliente == null)
{
return HttpNotFound();
}
AnamineseViewModel anamnese = new AnamineseViewModel();
anamnese.CliCliente.CliId = cliente.CliId;
return View(anamnese);
}
// POST: Anaminese/Edit/5
[HttpPost]
public ActionResult Edit(AnamineseViewModel anamneseViewModel)
{
if (ModelState.IsValid)
{
db.Entry(anamneseViewModel.CliCliente).State = EntityState.Modified;
db.Entry(anamneseViewModel.Tabeka2).State = EntityState.Modified;
db.Entry(anamneseViewModel.Tabela3).State = EntityState.Modified;
db.Entry(anamneseViewModel.Tabela4).State = EntityState.Modified;
db.Entry(anamneseViewModel.Tabela5).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(anamneseViewModel);
}
The view is the same as create, and a question has now come to me as well ... In edit views already created by asp.net mvc when doing scaffolding, there is a hidden field that takes the Id of the data you want to edit , in the ViewModel, do you need it too?
Could anyone help me? Because it is already giving error in the GET and I do not know how to solve!