3
I am developing a register, 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 get the inputs
of Model
.
So far so good, now, for example, if I want to add one more 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 my button #addItem
go on my Controller
and return a PartialView
of my inputs
, follows the 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’t 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 don’t know if it’s right. I hope you understand my problem, thank you and I look forward to your help.
I let you try to understand. You have a view and want to add new fields to html, and when you save, do you have to go to all these fields? 1 - If the fields are all previously known, it is easier. You don’t even have to return partialview. Create the new fields via jquery. In your POST, you send the list as you do.
– jpgrassi
@jpgrassi Good morning, that’s right, I’m creating my fields via
jQuery
only that there in the controller he can not read the ones I created. when you speak in my POST, is in @Html.Beginform or in the POSTAJAX
?– Furlan
So, I believe that what you are trying to do (pass the model at the time of the return of partialview) will not work. I have already implemented something similar but I used Angularjs. I was adding the fields on the normal screen via javascript and adding the "authors" in an array of objects. When I was going to save, I would send a POST by passing the list of authors to my controller.
– jpgrassi
I did some research, maybe this will help: http://stackoverflow.com/questions/15148103/mvc-4-how-do-i-pass-model-datato-a-partial-view
– jpgrassi
@jpgrassi I tried here but it didn’t work, do you have skype? or we can chat?
– Furlan