Problem sending model to controller

Asked

Viewed 603 times

3

I’m having trouble forwarding my model to 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 into mine Salvapessoa() method, except for my list changed the Isativo bool inside the form;

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

Can anyone help me on how to make this my list be sent with Salvapessoa()?

  • The way you are doing, the form will send a bool array to the controller.

  • Like I’d have to do in this case?

  • This link helped me make an implementation that sends a model with a list to the controller. https://www.codeproject.com/Tips/855577/List-of-Model-Object-Post-to-Controller-in-ASP-NET

3 answers

5


The right thing 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 the 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"})
}

Done so, the bind in the Controller will appear correct.

  • I passed as explained, but the list keeps coming null

  • I was thinking, after I did that, I wouldn’t have any way to apply my ListaClientes to the model in my View? Any idea how to do this?

  • If you did everything right, MVC does it automatically. If something is missing, you will receive the null list.

  • 1

    Thanks, it worked right now, I was really passing the wrong list lstCliente so it didn’t work. I replaced it with ListaClientes and now working perfectly... @Ciganomorrisonmendez

3

You can put 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++;
}

0

Add a List property to the Person object. So when rendered the object gets under control with its information.

In the controller add a new parameter to receive your list.

public JsonResult SalvaPessoa(Pessoa model, List<Pessoa> Pessoas){
    ...
}

And in the View within the form tags mounts the list that will be sent.

@using (Html.BeginForm("SalvaPessoa", "Home", FormMethod.Post))
{
    @Html.Hidden(m => m.Id)
    @Html.Hidden(m => m.Nome)
    @Html.Hidden(m => m.Tipo)

    @for(int i = 0; i < lstCliente.Count; i++){

        Cliente
        @Html.CheckboxFor(m => Model.Pessoas[i].IsAtivo);
        ...
    }

    Salvar
}

Remember that the list objects will be mounted only with the information that was built inside the for, in your case only the "isAtivo".

For more information check out this link: https://www.codeproject.com/Tips/855577/List-of-Model-Object-Post-to-Controller-in-ASP-NET

Browser other questions tagged

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