What do I get for getting one Model
in a Action
is you have the representation of each property of that Model
ma view and when the request to Action
is made the framework of ASP.NET MVC will effect the parser fields to an instance of Model
.
As you have already commented, create each field you want to move from each item in the list on view
so that you can receive a List<Model>
easy to manipulate in Action
is something painful for the environment. Especially if you have no way to predict the size that your lists can reach.
If your list receives values typed by the user and so you need to post all the Model
, then imagining as in an order screen in which each product is filled by the user, maybe post item by item is painful.
If that’s not it, if you just pass the list to the View
and then just need to know what the items on that list were when it was posted to Action
, then you can really solve it differently, with a list of Id s.
You at the time of setting up the view can create something like:
@model List<BDOleoTorres.Models.AutoDeclaracoesCertISCC>
...
@foreach (var I=0; I<Model.Count(); I++)
{
<input type="hidden" id="modelId_@I" name="modelId[@I]" />
}
@* Outros campos de forma que não precisam ser postados *@
....
In his Action
you get so:
public ActionResult downloadListaISCC(int[] modelId)
{
// recupera sua lista do banco de dados
// processa a lista
}
That way, you’ll surely be saving resources.
If that list is about exclusions and additions in View
and then it will depend on what the user will do, so it won’t be right to try to define the index of the fields to render, but to set the indexes before posting.
Example:
@model List<BDOleoTorres.Models.AutoDeclaracoesCertISCC>
...
@foreach (var I=0; I<Model.Count(); I++)
{
<input type="hidden" id="modelId" name="modelId" />
}
@* Outros campos de forma que não precisam ser postados *@
....
Before posting, you would do something like:
$("#seuForm").submit(function () {
var idx = 0;
$("#modelId", this).each(function () {
var $self = $(this);
$self.attr("id", $self.attr("id") + "_" + idx);
$self.attr("name", $self.attr("name") + "[" + idx + "]");
idx++;
});
});
This is necessary because if there is a cut in the index sequence your list will not be passed to the Model
in Action
completely.
Following this idea, you can add up to more than one field, for each of them you create a parameter as vector in Action
, or you can opt for a ModelView
, so it would look more "elegant".
I think you should just keep the identifier of the object and not the whole object, it would be something much more "beautiful". And in
downloadListaISCC
consult the object to generate thePDF
.– Fernando Leal
How do I store only the object identifier?
– CesarMiguel