How to write data in a one-to-many table?

1

I have a question that still persists after doing some searches.

It is as follows, assuming I have a form, where I register multiple emails for a single user, how do I record these multiple emails in their table? Here are the examples: (if you have any errors please feel free to correct)

Models and ViewModel:

 public class Email
{
    public int EmailID { get; set; }
    public int UsuarioID { get; set; }
    public string Email { get; set; }

    [ForeignKey("UsuarioID")]
    public virtual Usuario Usuarios { get; set; }
}

public class Usuario
{
    public int UsuarioID { get; set; }
    public string Nome { get; set; }

    public virtual ICollection<Email> Emails { get; set; }
}

public class UsuarioNovo
{
    [Required]
    public int Nome { get; set; }

    [Display(Name = "E-mail")]
    public string Email { get; set; }
}

In html, I created a div with the class .dc-box-clonar with the inputs and a script in js to clone the div when clicking on a certain link, it follows the script:

$('.dc-box-clonar').hide();
$('.control-add-box').on('click', function (e) {
    e.preventDefault();
    var newElem = $(this).parent().find('.dc-box-clonar:first').clone();
    newElem.find('input').val('');
    newElem.prependTo($(this).parent()).show();
    var height = $(this).prev('.dc-box-clonar').outerHeight(true);

    $("html, body").stop().animate({ scrollTop: $(this).offset().top - 520 }, 600);
});

What would the Controller look like with EF instructions? And this way of cloning in js right on this occasion?

    
asked by anonymous 10.11.2015 / 13:35

1 answer

1

Your question is one more of the classic questions that ask you to use the BeginCollectionItem , but there are some additions to make.

The naming convention for Models is usually in the singular, because each Model identifies a entity in the singular :

public class Email
{
    public int EmailID { get; set; }
    public int UsuarioID { get; set; }
    public string Email { get; set; }

    [ForeignKey("UsuarioID")]
    public virtual Usuario Usuario { get; set; }
}

Another thing is that this is the mapping of cardinality 1 to N. That is, in Usuario you can declare that the entity has several Email :

public class Usuario
{
    public int UsuarioID { get; set; }
    public string Nome { get; set; }

    // Essa declaração agora sim é no plural.
    public virtual ICollection<Email> Emails { get; set; }
}

The script to clone the div is apparently ok, but there are some details you need to try:

  • If you are going to use BeginCollectionItem , cloning needs to go through Controller to define the temporary key (which is recommended);
  • If you do not use it, you will need to set its <form> so that the name attribute of each <input> is identified by an index (preferably numeric). Additionally, this index must be contained in an additional field in <form> called index . For example:

                                      

What BeginCollectionItem does this: create this <form> scheme automatically for you.

With this, you can only use this in Controller :

[HttpPost]
public ActionResult Salvar(Usuario usuario) 
{
    /* Usuario.Emails estará preenchido com os e-mails da tela. */
}
    
10.11.2015 / 15:16