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?
Thanks Master Gypsy! haha But what do you mean by : cloning needs to go through the Controller?
– Matheus Silva
Javascript calls a Partial that sets the right line for you. If you need examples in this, open another question.
– Leonel Sanches da Silva
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
– Matheus Silva
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."
– Matheus Silva
Yes, you can start the
ICollection
with an empty object.– Leonel Sanches da Silva
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.
– Matheus Silva
Now I think it’s a great time to open up another question.
– Leonel Sanches da Silva
Then wouldn’t you be able to supplement that answer anyway? Because basically the question would remain the same...
– Matheus Silva
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.
– Leonel Sanches da Silva