Action Edit in ViewModel

0

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!

    
asked by anonymous 08.06.2016 / 05:38

2 answers

2

In my opinion, I do not think you understood any of the explanations.

This will not work:

AnamineseViewModel anamnese = new AnamineseViewModel();

anamnese.CliCliente.CliId = cliente.CliId;

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

AnamineseViewModel anamnese = new AnamineseViewModel 
{
    CliCliente = cliente
};
  

In edit views already created by asp.net mvc when doing scaffolding, has a hidden field that takes the Id of the data you want to edit, in the ViewModel, do you need too?

Of course, yes, because you are doing this:

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.

    
08.06.2016 / 05:52
-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 either DbSet of CliClient in DbContext something like 'public DbSet CliClient> (without the quotation marks) clients {get; set;} 'if this were the way you would put' db.clients.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

    
08.06.2016 / 19:07