How to insert an object and a list of objects in the same request

0

I have a supplier object, and each supplier has several contacts, on the page, has a tab for the supplier register, and another tab for the contact register, the contact register tab has a small form, and below one table where to every item registered in the form, I insert into the table using JavaScript. After this page has a tab filled with vendor data and on the other tab an HTML table with a contact list, I need to send this to controller to register with the bank.

In my View I use a ViewModel Like this:

public class FornecedorContatoViewModel
{
    public Fornecedor Fornecedor { get; set; }
    public Contato Contato { get; set; }
}

The form is made with razor, and the table is HTML itself.

In the controller, something like this would work:

public ActionResult Add(Fornecedor fornecedor, IList<Contato> contato)
{

}

And how to make the whole table go to the action in the form of a list (List in this case), how would the submit, would it do normally?

    
asked by anonymous 23.11.2016 / 18:20

1 answer

1

It would work, though it does not make the least sense. Once you are using ViewModels, you can simply use it as a parameter to your action.

public ActionResult Add(FornecedorContatoViewModel fornecedorContatoVm)
{

}

Otherwise, your ViewModel is wrong. It should have List<Contato> instead of Contato

public class FornecedorContatoViewModel
{
    public Fornecedor Fornecedor { get; set; }
    public List<Contato> Contatos { get; set; }
}
    
23.11.2016 / 19:16