Problem sending model to controller

3

I'm having trouble forwarding my model to the control:

Class:

    public class Pessoa {
        public virtual long Id {get; set;}
        public virtual string Nome {get; set;}
        public virtual ETipo Tipo {get; set;}

        public virtual List ListaClientes {get; set;}
    }

View:

    @using Projeto.Model.Enum
    @model projeto.Model.Cadastro.Pessoa
    @{
        title = ".."

        List lstCliente = Model.ListaClientes;
    }

    @using (Ajax.BeginForm("SalvaPessoa", "Home", new AjaxOptions { HttpMethod = "POST"})) {

        @Html.Hidden(m => m.Id)
        @Html.Hidden(m => m.Nome)
        @Html.Hidden(m => m.Tipo)

        @foreach(var x in @lstCliente){

            Cliente
            @Html.CheckboxFor(m => m.ListaClientes.IsAtivo, new{ class = "form"})

        }

        Salvar

        

        
    }

Controller:

    public JsonResult SalvaPessoa(Pessoa model){
        ...
    }

So, all the fields are coming right on my (), except for my list whose changed the IsActive bool inside the form;

All fields are valued, even the list already comes from my ActionResult () Valued.

Can anyone help me with how to make this my list is sent along to the PersonalSave ()?

    
asked by anonymous 31.03.2017 / 19:17

2 answers

4

The correct one would be:

public class Pessoa 
{
    public virtual long Id {get; set;}
    public virtual string Nome {get; set;}
    public virtual ETipo Tipo {get; set;}

    public virtual List<Cliente> ListaClientes {get; set;}
}

Another thing is to assemble the form with BeginCollectionItem :

@using (Ajax.BeginForm("SalvaPessoa", "Home", new AjaxOptions { HttpMethod = "POST"})) {

    @Html.Hidden(m => m.Id)
    @Html.Hidden(m => m.Nome)
    @Html.Hidden(m => m.Tipo)

    @foreach(var x in @lstCliente)
    {
        @Html.Partial("_Cliente", x)
    }

}

_Cliente.cshtml

@model SeuProjeto.Models.Cliente

@using (Html.BeginCollectionItem("ListaClientes"))
{
    Cliente
    @Html.CheckboxFor(m => m.IsAtivo, new { @class = "form"})
}

bind in Controller will appear correct.

    
31.03.2017 / 19:59
3

You can for all client properties with the html tag <input type="hidden"/>

int index=0;

@foreach (var x in @lstCliente)
{
    //Cliente
    <input type="hidden" name="ListaClientes[@index].Nome" value="@x.Nome" />
    <input type="hidden" name="ListaClientes[@index].Documento" value="@x.Documento" />

    @Html.CheckboxFor(m => m.ListaClientes.IsAtivo, new { class = "form"})
    index++;
}
    
31.03.2017 / 19:45