1
I have a viewmodel called Profile, in this viewmodel there is a Itensprofile List property and a string - title.
public class PerfilViewModel
{
[Key]
[DisplayName("Código")]
public int Id { get; set; }
[Required(ErrorMessage = "O campo {0} é obrigatório")]
[StringLength(50, ErrorMessage = "O campo {0} deve ter no mímino {1} caracteres")]
[DisplayName("Título")]
public string Titulo { get; set; }
public IEnumerable<PerfilItens> ItensPerfil { get; set; }
}
The attributes of the Itensprofile class are boleanos: visualize, create, edit and delete and one more integer (id) that indicates the Profile of this Itensprofile. I create my view this way:
var viewModel = new PerfilViewModel();
var itens = Bootstrap._listaPerfil;
viewModel.ItensPerfil = itens.Select(x => new PerfilItensViewModel
{
ItemMenu = x.ItemMenu,
Criar = x.Criar,
Visualizar = x.Visualizar,
Excluir = x.Excluir,
Editar = x.Editar
});
return View(viewModel);
The Bootstrap function. _listPerfil() only loads the profile items that should be created. The view appears perfectly, through a for or foreach I can generate for the user of the profile items, he just need to enter a title and mark or not, via checkbox, the items.
<form asp-action="Create">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="Descricao" class="control-label"></label>
<input asp-for="Descricao" class="form-control" />
<span asp-validation-for="Descricao" class="text-danger"></span>
</div>
@foreach (var item in Model.ItensPerfil)
{
<hr />
<div class="form-group">
<label id="menu" class="control-label">@item.ItemMenu</label>
</div>
<div class="form-group">
<label asp-for="@item.Visualizar" class="control-label"></label>
<input type="checkbox" asp-for="@item.Visualizar" checked="@item.Visualizar" />
<label asp-for="@item.Editar" class="control-label"></label>
<input type="checkbox" asp-for="@item.Editar" checked="@item.Editar" />
<label asp-for="@item.Criar" class="control-label"></label>
<input type="checkbox" asp-for="@item.Criar" checked="@item.Criar" />
<label asp-for="@item.Excluir" class="control-label"></label>
<input type="checkbox" asp-for="@item.Excluir" checked="@item.Excluir" />
</div>
}
<hr />
<div class="form-group">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</form>
</div>
Now let’s get to the problem, when it returns to the Create by Post function the Profile comes correctly, the title appears right, but the itensPerfil returns as null. Some solution to this?
In the method where you select the data, you are giving
Include
ofItensPerfil
. Post also the code of the selection method..– Barbetta
Put the code in the question.
– Barbetta