Action Edit in Viewmodel

Asked

Viewed 88 times

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!

2 answers

2


The way I see it, I don’t think you understand any of the explanations.

This isn’t gonna work:

AnamineseViewModel anamnese = new AnamineseViewModel();

anamnese.CliCliente.CliId = cliente.CliId;

CliCliente will be null, most likely. Nothing in your code shows that CliCliente will be instantiated. The correct would be:

AnamineseViewModel anamnese = new AnamineseViewModel 
{
    CliCliente = cliente
};

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?

Of course, because you’re doing it:

db.Entry(anamneseViewModel.CliCliente).State = EntityState.Modified;
db.Entry(anamneseViewModel.Tabela2).State = EntityState.Modified;
db.Entry(anamneseViewModel.Tabela3).State = EntityState.Modified;
db.Entry(anamneseViewModel.Tabela4).State = EntityState.Modified;
db.Entry(anamneseViewModel.Tabela5).State = EntityState.Modified;

Without the corresponding Id of each entity, none of these commands will work.

  • question: where has CliCliente = cliente where cliente is to take the client ID, referenced in the action signature (int? id), I could use for all my views this id?

  • I didn’t understand a word you said. cliente comes from CliCliente cliente = db.CliCliente.Find(id);. What do you mean "I could use for all my Views"?

  • 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

  • Basically using Begincollectionitem. Here are some answers on how to use.

-1

Try this:

    [HttpGet]        
    public ActionResult Edit(int? id)
    {           

        CliCliente cliente = db.x*.Find(id);
        if (cliente == null)
        {
            return HttpNotFound();
        }

    AnamineseViewModel anamnese = new AnamineseViewModel();

    anamnese = cliente.CliCliente.CliId;

    return View(anamnese);

    }

Obs: In this "x*" you have to put the Dbset of Clicliente in the Dbcontext something like 'public Dbset<"Clicliente"> (without the quotes) clients { get; set;}' if it were this way you would put 'db.clientes.Find(id)'

[HttpPost]
public ActionResult Edit(AnamineseViewModel model) //mudei para model para você não se confundir com o Model.AnamineseViewModel
{
    if (ModelState.IsValid)
    {
        db.Entry(model.CliCliente).State = System.Data.Entity.EntityState.Modified;
        db.Entry(model.Tabela2).State = System.Data.Entity.EntityState.Modified;
        db.Entry(model.Tabela3).State = System.Data.Entity.EntityState.Modified;
        db.Entry(model.Tabela4).State = System.Data.Entity.EntityState.Modified;
        db.Entry(model.Tabela5).State = System.Data.Entity.EntityState.Modified;

        db.SaveChanges();
        return RedirectToAction("Index");

    }

    return View(anamneseViewModel);
}

Maybe the error is occurring because you are trying to change the database without System.Data.Entity before Entitystate

Browser other questions tagged

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