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

Asked

Viewed 468 times

0

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

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 even.

In the controller, something like:

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), as would be the Submit, could do normally?

  • Yes, and the controller code is absolutely correct.

  • @jbueno, you’re right, or you’re being sarcastic?

  • That’s right, young man. I don’t use the site to be sarcastic. Of course there are other ways to do it, some even more indicated depending on the context of the project. But this way it will work perfectly.

  • Thank you very much, you see, it’s just that I’ve been sarcastic with myself here, and I figured that wouldn’t work. If you have any link to any article about the most indicated ways to do this as you mentioned, post as an answer that then I already mark your answer. Thank you.

  • The intention is not to have this kind of behavior here. Whenever it happens you can flag the comment (or the post). The site has a team of moderators that can take care of it. About answer: responses only with links are not well seen (or useful for the site). I could create an answer for you, but first I need to understand what the problem itself is. You just want an example of how you can send two parameters to controller? If you can make the question clearer, it would be nice [Dit]. [+]

  • [+] This way the question can be useful for anyone who has the same + doubt in the future and you will get your answer. In addition, other users may publish new responses showing other ways to do so, etc.

  • OK I’ll edit the question and try to make it clear.

  • So. It looks better with your editing. Now my idea is different. You don’t need two parameters in the action Add. You only need one FornecedorContatoViewModel. It still works at first, but I see no point in following two different patterns. If you’re using with ViewModel follow this path and be happy.

  • Really, now that you’ve spoken it makes more sense anyway. Thank you for your help

  • I was thinking here now, using Viewmodel, how it has a property Contact and not a List<Contact>, would it receive several contacts inserted in the table?

  • So. I was writing about it. Take a look at the answer.

Show 6 more comments

1 answer

1


It would work, although it doesn’t make any sense. Once you are using Viewmodels, you can simply use it as a parameter of your action.

public ActionResult Add(FornecedorContatoViewModel fornecedorContatoVm)
{

}

Other than that, her Viewmodel is wrong. She should have one List<Contato> instead of Contato

public class FornecedorContatoViewModel
{
    public Fornecedor Fornecedor { get; set; }
    public List<Contato> Contatos { get; set; }
}
  • Okay, but then I can’t do this: @Html.Editorfor(model => model.Contacto.Email) I could do it in HTML with no problem but what would the name of the field look like, name = "Contact.Email"? I accidentally hit enter... :)

  • @Alanalmeida You will have to scroll through the list to show the values, normal. At first you could only have a contact for each supplier, that does not seem to me to be the intention.

  • If the idea is to have several contacts, I would have to go through the list in the right controller? To get each contact that came from the view. What I wanted to know is for Asp.net to make the Binding data the name attributes of HTML fields need to be the same name as the object property that the action receives, right? In case, if I use Razor when I model. to display the properties list of my obj, it shows me the list of methods that List<> has, so I said to do in HTML, in this case, the name attribute of the HTML fields of the contact fomr would be like to do the Binding correctly?

  • Just do a foreach on the view.

  • Okay, thank you so much for your help.

Browser other questions tagged

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