Recently, I asked a question about my problem, but it was kind of difficult to understand, here I will try to summarize my problem.
I'm using ASP.NET MVC.
I have 2 tables: Autores
and Livros
, where 1 Autor
has many Livros
.
Autor
and want to add another Livro
, I have a button that creates my fields via jQuery
, so when I save, my Controller
takes only the 1st name, the other names that I added by jQuery
are not added correctly.
Then we go to the screen to register author (example):
<div class="col-xs-12 col-md-4">
@Html.LabelFor(model => model.Nome)
@Html.EditorFor(model => model.Nome)
</div>
<div class="col-xs-12 col-md-4">
@Html.LabelFor(model => model.NomeLivro)
@Html.EditorFor(model => model.NomeLivro)
</div>
//DIV PARA RECEBER OS NOVOS CAMPOS
<div id="divAddLivro"></div>
Then I have my button for the jQuery call:
<button id="btnAddLivro">Adicionar Livro</button>
My jQuery
:
$("#btnAddLivro").click(function () {
$("#divAddLivro").append(''+
'<div class="col-xs-12 col-md-4">'+
' @Html.LabelFor(model => model.Nome)'+
' @Html.EditorFor(model => model.Nome)'+
'</div>'+
'<div class="col-xs-12 col-md-4">'+
' @Html.LabelFor(model => model.NomeLivro)'+
' @Html.EditorFor(model => model.NomeLivro)'+
'</div>' +
'</div>');
});
MODEL AUTHOR
public class Autor
{
[Key]
public int IdAutor { get; set; }
public string Nome { get; set; }
public ICollection<Livro> Livros{ get; set; }
}
MODEL BOOK
public class Livro
{
[Key]
public int IdLivro { get; set; }
public string Nome { get; set; }
public int IdAutor { get; set; }
public ICollection<Autor> Autores { get; set; }
}