The best way is to pass the user group back to Action POST
:
[HttpPost]
public ActionResult Salvar(GrupoDeUsuario grupoDeUsuario)
{
/* Lógica de negócios aqui */
}
Possibly your binding problem is related to the fact that ModelBinder
does not send the values of each function bound to the user group back to Controller . For this to happen, you need to have two things in your form
:
Each role record must be identified with an index;
The fields of each function should be indexed by the value contained in this index field.
In summary, 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
. For simplicity, I'll use integers:
<input type="hidden" name="Funcoes.index" value="1" />
Now, the checkbox
fields. You did not say their names, so I'll call them Campo1
, Campo2
, Campo3
, Campo4
. It 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 />
This alone is enough for Action to recognize the values when submitting the form. It's just a bit of a long story, so it's better to use a tool for it. In this case, her name is BeginCollectionItem . I have answered it over and over again . What it does is generate this index field and index the fields for you, but it works fine 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)
}
}