Edit two tables simultaneously in an "edit" view

2

I have two tables: Pessoa and Catequizando , with a ratio of 1: 1 and I want to get a "Get" to the data of the two tables, both "id's" are equal, >

Catequizando catequizando = db.Catequizando.Find(id);
Pessoa pessoa = db.Pessoa.Find(id);

I do not know how to do return view of the 2 tables.

Controller:

  public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Catequizando catequizando = db.Catequizando.Find(id);
            Pessoa pessoa = db.Pessoa.Find(id);
            if (catequizando == null)
            {
                return HttpNotFound();
            }

            ViewBag.CatequizandoID = new SelectList(db.Pessoa, "PessoaID", "Nome", catequizando.CatequizandoID);
            return View(catequizando);
        }
    
asked by anonymous 27.08.2015 / 18:33

1 answer

2

You can create an intermediate class (view model), where you will have two attributes, one of type Catechizing and Person example:

public class CadastroViewModel
{
    public Catequizando catequizando { get; set; }        
    public Pessoa pessoa { get; set; }
}

Now pass this class on the model of your view, and then receive the same in your controller. To access the attributes in the view just do the normal process:

@model caminho.CadastroViewModel

@Html.EditorFor(model => model.catequizando.SEUATRIBUTO)
@Html.EditorFor(model => model.pessoa.SEUATRIBUTO)

In your case it would look like this:

public ActionResult Edit(int? id)
{
    if (id == null)
    {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }

    Catequizando catequizando = db.Catequizando.Find(id);
    Pessoa pessoa = db.Pessoa.Find(id);

    CadastroViewModel cadastro = new CadastroViewModel();
    cadastro.catequizando = catequizando;
    cadastro.pessoa = pessoa;

    if (catequizando == null)
    {
        return HttpNotFound();
    }

    ViewBag.CatequizandoID = new SelectList(db.Pessoa, "PessoaID", "Nome", catequizando.CatequizandoID);
    return View(cadastro);
}
    
27.08.2015 / 18:45