How to write data in one table for many?

Asked

Viewed 255 times

1

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

Is the following, assuming I have a form, where I register several emails to a single user, how I record these several emails in their table? Follow the examples: (if you have any errors 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 inputs and a script in js to clone the div by clicking on a given link, follow 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 Controller look like with EF instructions? And this way of cloning in js is right on this occasion?

1 answer

1


Your question is another of the classic questions that ask the use of Begincollectionitem, but there are some addendums to be made.

The naming convention of Models usually it is in the singular, because each Model identifies an 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. IE 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 of cloning the div is apparently ok, but there are some details you need to look into:

  • If you’re gonna use the Begincollectionitem, cloning needs to go through Controller to set the temporary key (what is recommended);
  • If you do not use, you will need to define your <form> so that the attribute name of each <input> is identified by an index (numerical preference). In addition, this index needs to be contained in an additional field in the <form> called index. For example:

What the Begincollectionitem does this: create this <form> automatically for you.

With this, you can use on Controller only this:

[HttpPost]
public ActionResult Salvar(Usuario usuario) 
{
    /* Usuario.Emails estará preenchido com os e-mails da tela. */
}
  • Thanks Master Gypsy! haha But what do you mean by : cloning needs to go through the Controller?

  • Javascript calls a Partial that sets the right line for you. If you need examples in this, open another question.

  • 1

    Okay, I’m going to do a little research here, and in case I can’t find anything or I’m not getting it, I’m going to jump in here

  • In Controller I need to declare what is going on Icollection and start it along with the view? For here when I give show the Partialview in the main View is returned a Nullreferenceexception: "Object reference not defined for an instance of a."

  • Yes, you can start the ICollection with an empty object.

  • But when I try to start with Icollection, VS points out that it cannot instantiate an abstract class. With List it does not point out this exception.

  • Now I think it’s a great time to open up another question.

  • Then wouldn’t you be able to supplement that answer anyway? Because basically the question would remain the same...

  • Not because you’ve already started having mistakes, and I wish I could read your screen now, but I can’t. So please open another question containing exactly the problem you are having.

Show 4 more comments

Browser other questions tagged

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