The best way is to move the user group back to the Action POST
:
[HttpPost]
public ActionResult Salvar(GrupoDeUsuario grupoDeUsuario)
{
/* Lógica de negócios aqui */
}
Possibly your problem of Binding is related to the fact that the ModelBinder
does not send the values of each function linked to the user group back to the Controller. For that to happen, you need to have two things in your form
:
- Each function record needs to be identified with an index;
- The fields of each function shall be indexed by the value contained in this index field.
In short, the index of a function is a <input type="hidden">
anywhere on the form whose name is Funcoes.index
and value an integer or Guid
. To simplify, I’ll use integers:
<input type="hidden" name="Funcoes.index" value="1" />
Now, the fields checkbox
. You didn’t say their names, so I’ll call them Campo1
, Campo2
, Campo3
, Campo4
. Would look like this:
<input type="hidden" name="Funcoes.index" id="Funcoes_index" value="1" />
<input type="checkbox" name="Funcoes[1].Campo1" id="Funcoes_1_Campo1" value />
<input type="checkbox" name="Funcoes[1].Campo2" id="Funcoes_1_Campo2" value />
<input type="checkbox" name="Funcoes[1].Campo3" id="Funcoes_1_Campo3" value />
<input type="checkbox" name="Funcoes[1].Campo4" id="Funcoes_1_Campo4" value />
Just this is enough so that the Action recognize the values when submitting the form. Only this is a bit long to do, so it is better to use a tool for this. In which case, her name is Begincollectionitem. I’ve answered several times about her. What it does is generate this index field and index the fields for you, but it works well using Razor. It would look like this:
@foreach (var funcao in Model.Funcoes)
{
using (Html.BeginCollectionItem("Funcoes"))
{
@Html.CheckBoxFor(_ => funcao.Campo1)
@Html.CheckBoxFor(_ => funcao.Campo2)
@Html.CheckBoxFor(_ => funcao.Campo3)
@Html.CheckBoxFor(_ => funcao.Campo4)
}
}