Return of a List From View to the Controller - Asp.Net Core

1

I have a viewmodel called Profile, in this viewmodel there is a ListProperties 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 ProfileStyle class are boolean: view, create, edit, and delete, and one more (id) that indicates the Profile of this ProfilePart. 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.ListPath () function only loads the profile items that should be created. The view appears perfectly, through a for or foreach to generate the user of the profile items, it just needs to insert a title and check 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 we go to the problem, when it returns to the Create function by Post the Profile comes correctly, the title appears right, but the Profile items returns as null. Any solution for this?

    
asked by anonymous 27.03.2018 / 22:50

2 answers

0

Solved. In this case you can not use List because in HTML the IDs in the inputs inside the loop are repeated and cause the problem, you must use vector.

    
17.04.2018 / 14:32
0

Barbetta, would that be what you wanted to see?

public class Bootstrap
{        
    public static List<PerfilGenerico> _listaPerfil { get; private set; }

    public static void Configuracao(IServiceCollection services)
    {
        SetListaPerfil();

        services.AddScoped<IAmbiente, AmbienteRepositorio>();
        services.AddScoped<IAmbienteApp, AmbienteApp>();

        services.AddScoped<ICidade, CidadeRepositorio>();
        services.AddScoped<ICidadeApp, CidadeApp>();

        services.AddScoped<IEstado, EstadoRepositorio>();
        services.AddScoped<IEstadoApp, EstadoApp>();

        services.AddScoped<IPerfil, PerfilRepositorio>();
        services.AddScoped<IPerfilApp, PerfilApp>();

        services.AddScoped<IPerfilItens, PerfilItensRepositorio>();
        services.AddScoped<IPerfilItensApp, PerfilItensApp>();
    }        

    private static void SetListaPerfil()
    {
        _listaPerfil = new List<PerfilGenerico>
        {
            new PerfilGenerico { Classe = "Ambiente" },
            new PerfilGenerico { Classe = "Cidade" }
        };
    }
}

 public class PerfilGenerico
{
    public string Classe { get; set; }
    public bool Visualizar { get; set; }
    public bool Criar { get; set; }
    public bool Editar { get; set; }
    public bool Excluir { get; set; }
}
    
28.03.2018 / 14:58