What I understand by receiving a Model
in a Action
is that you have the representation of each property of that Model
ma view and when the request to Action
is made the framework ASP.NET MVC will parse the parser fields for an instance of Model
.
As you've already commented, creating each field that you want to traffic from each list item in view
so that you can receive an List<Model>
easy to handle Action
is somewhat painful for the environment. Especially if you can not predict what size your lists can reach.
If your list receives values typed by the user and therefore you need to post all Model
, then wondering how in a request screen where each product is populated by the user, maybe posting item to item is less painful
If it is not, if you just pass the list to View
and then just need to know what the items in that list were when it is posted to Action
, then you can actually solve it in another way, with a list of Id's.
At the time of mounting the view you 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 your Action
you get:
public ActionResult downloadListaISCC(int[] modelId)
{
// recupera sua lista do banco de dados
// processa a lista
}
That way you're sure to be saving money.
If this list is about exclusions and additions in View
and then it will depend on what the user is doing, then it will not work to set the index of the fields to render, but rather to index 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 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 a vector in Action
, or you can opt for a ModelView
, so it would look more "elegant". >