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

Asked

Viewed 339 times

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 of ItensPerfil. Post also the code of the selection method..

  • Put the code in the question.

2 answers

0


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

  • Do not forget to mark the question as answered. https://pt.meta.stackoverflow.com/questions/1078/como-e-por-que-aceitar-uma-resposta/1079#1079

0

Barbetta, that’s 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; }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.