4
Recently I asked a question about my problem, but it got a little hard 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
.
When you create the Autor
and want to add one more Livro
, I have a button that creates my fields via jQuery
, only when I’m saving mine Controller
takes only the first name, the other names I added by jQuery
are not correctly added.
So let’s 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>
There’s my jQuery call button:
<button id="btnAddLivro">Adicionar Livro</button>
mine 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; }
}
What are you using for the web part? Asp.net MVC, Webforms, Angular, etc, etc, etc, etc... Could post the code of how you are doing this?
– Randrade
OK @Randrade I will specify
– Furlan
@Randrade changed the question, thank you
– Furlan