I'm developing a registration, where I have my fields according to my model:
public class Autor
{
[Key]
public int IdAutor { get; set; }
public string Nome { get; set; }
public DateTime DataNascimento { get; set; }
}
That is, in my file .cshtml
will have the inputs
of Model
.
So far so good, now, for example, if I want to add another Autor
dynamically, without leaving the page, using AJAX
, in fact, make the call:
$(document).ready(function() {
$("#addItem").click(function () {
$.ajax({
type: 'GET',
url: '@Url.Action("AdicionarNovoAutor", "Autores")',
success: function (html)
{
$("#addnovo").append(html);
},
error: function (html)
{
console.log("Erro");
}
});
});
});
So, when I click on my button #addItem
I go to my Controller
and return a PartialView
of my inputs
, follow PartialView
:
@model List<MVC1.Models.Autor>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model[0].Nome, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model[0].Nome, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model[0].Nome, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model[0].DataNascimento, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model[0].DataNascimento, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model[0].DataNascimento, "", new { @class = "text-danger" })
</div>
</div>
</div>
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
This code goes to my div, but in my Controller
I can not get the ones I added, follow the Controller
when I go to Create
:
public ActionResult AdicionarNovoAutor(List<Autor> autores)
{
autores.Add(new Autor());
return PartialView("~/Views/Autores/_AutorPartial.cshtml", autores);
}
In this case, I'm trying to pass as a parameter a list of Authors, I do not know if it's right. I hope you understand my problem, thank you and I look forward to your help.