0
Well, I’m trying to implement the action of editing the data of a viewmodel that I own in my project.
I even put together an action, but it’s not working... What happens is that what I researched, when you build an Edit action, you need to reference the model’s ID to load the data... What happens is how is Viewmodel, I didn’t map it in the database, so that way I can not get the Id.
I have in my project several tables, as I say in that question of mine, where, to reinforce, I have several models that are in Viewmodel.
In other words, I have the model Clicliente, Table2, Table3, Table4 and Table5. Where the clicliente relates to Table 2. Table 2 relates to the others (Table 3, 4 and 5). Or, to be more clear, Tables 3, 4 and 5 relate to Table 2.
The part of the data registration in the system is already working, but now I need to edit.... The way I’m trying is, I show the client’s name and from there, I take all the other data. That is, I wanted to show the client name and when the user clicks to 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 I have a question now too... In the editing views already created by Asp.net mvc when scaffolding, you have a Hidden field that takes the Id of the data you want to edit, in Viewmodel, you also need?
Could someone help me? Because I’m already screwing up on GET and I don’t know how to fix it!
question: where has
CliCliente = cliente
wherecliente
is to take the client ID, referenced in the action signature (int? id
), I could use for all my views thisid
?– Érik Thiago
I didn’t understand a word you said.
cliente
comes fromCliCliente cliente = db.CliCliente.Find(id);
. What do you mean "I could use for all my Views"?– Leonel Sanches da Silva
I have already solved this part... I have another problem... It is that in the models I have two that are lists.. I needed to know a way to get these values off the list
– Érik Thiago
Basically using Begincollectionitem. Here are some answers on how to use.
– Leonel Sanches da Silva