Complex type bind with Angularjs

Asked

Viewed 136 times

5

I am studying Angularjs and Asp.net MVC and could not bind a property CPF class Person when performing a POST:

Classe Pessoa:

public class Pessoa
{
   protected PessoaFisica() { }

   public Int32 PessoaFisicaID{ get; set; }
   public String Nome { get; set; }
   public Cpf CPF { get; set; } 
   ...

}

Class CPF:

public partial class Cpf
{
        protected Cpf() { }        
        public Int64 Numero { get; private set; }
        ...
}

Controller Angularjs:

...
vm.salvar = function () {

            var pessoaFisica = new Object();
            pessoaFisica.PessoaFisicaID = vm.PessoaFisicaID;
            pessoaFisica.Nome = vm.Nome;

            //Bind do CPF com problema :-(

            //Tentativa 1:
            //pessoaFisica.CPF = new Object();
            //pessoaFisica.CPF.Numero = vm.CPF; 

            //Tentativa 2:
            pessoaFisica.CPF = vm.CPF;

            //Uso o $http.post()
            ajaxService
                    .ajaxPost(
                        pessoaFisica,
                        '/Home/Criar',
                        this.salvarPessoaFisicaOnSuccess,
                        this.salvarPessoaFisicaOnError
                    );   
        }
        ...

My Canvas:

inserir a descrição da imagem aqui

At first try I used the code below, but it did not work:

//Tentativa 1:
pessoaFisica.CPF = new Object();
pessoaFisica.CPF.Numero = vm.CPF; 

inserir a descrição da imagem aqui

Error: System.Missingmethodexception: No constructor without parameters has been defined for this object. inserir a descrição da imagem aqui

In the second attempt I used the code below and also did not work:

//Tentativa 2:
pessoaFisica.CPF = vm.CPF;

Arrives null in Asp.net MVC controller: inserir a descrição da imagem aqui

What I might be adjusting to be able to make the CPF bind informed?

  • Tried to take the private of set of property Numero in class Cpf?

  • I adjusted the code according to the @Randrade response, but it continued with the same error. System.Missingmethodexception: No constructor without parameters has been defined for this object. After a long time trying to find out ("thrashing") I identified that the error occurs because the CPF class has a constructor protected, I was able to perform the bind by changing the access to public. But will the solution is to put public access even?

2 answers

4

You can solve this problem by creating the Object in this way:

var pessoaFisica = {
    id: 1,
    Nome: 'Renan',
    Cpf: {
        Numero: '232432'
    }
};

But this way you’re not using Angularjs itself, but creating a normal object in javascript.

Using Angular, your object would be something like this:

$scope.person = {
  'id': '1',
  'nome': 'Renan',
  'Cpf': {
     'Numero': '1234678'
  },
}

And that way you pass the $scope.person as an object.

$http.post("Pessoa/Criar/", { person : $scope.person});

On this website you have some examples of how to use Angular.

In this question you have some answers on how to use Angular for this purpose.

1


You need to adjust your Viewmodel to receive the same structure that is coming from your post request.

public class PessoaViewModel
{
    public int PessoaFisicaID { get; set; }
    public string Nome { get; set; }
    public CPF cpf { get; set; }
}
public class CPFViewModel
{
    public string Numero { get; set; }
}

See if it settles, and a return.

  • This would solve if my CPF class had a public builder. Look at the question she doesn’t have. As I added in the question, I was only able to bind after I changed the constructor of the CPF class protected for public. The question now is: Put the constructor of the CPF class as public is the solution to bind the person object? It would be impossible to do the post if my Person object if your Cpf does not have a public constructor?

  • @Renan, the CPF class should be Cpfviewmodel tbm. The idea is to build an environment that receives values via request, after which transport these values to its model classes.

  • But then in case I leave the CPF maker with access protected I’ll need to create this class again no Asp.net MVC (this time with contrutor public) to be able to bind? The other alternative would be to leave the CPF maker with public access so I don’t have to recreate this class (like the Cpfviewmodel), that would be it?

  • @Renan, you do not need to touch your class. Simply create a new class, called Cpfviewmodel, to receive the data coming from the request. Then you pass the data to an object in your CPF class.

Browser other questions tagged

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