How to use a padleft(11) in a get in the API

Asked

Viewed 100 times

-3

This is the method I have in my MVC

public async Task<List<FuncionarioViewModel>> GetFuncionariosVM()
        {
            string url = $"http://localhost:56137/api/GetFuncionario";
            var response = await client.GetStringAsync(url);
            var _funcionario = JsonConvert.DeserializeObject<List<FuncionarioViewModel>>(response);   


            return _funcionario;
        }

I need to give m padleft(11) in the CPF field and do not know how to do. Below the get method in the API

public class GetFuncionario
    {
        BancoContext banco = new BancoContext();

        //Método que retorna uma lista de funcionarios
        public List<Funcionario> GetFuncionarios()
        {
            return banco.Database.SqlQuery<Funcionario>("sp_cons_funcionarios").ToList();
        }
    }

Because it’s a numeric field, I need a padleft(11) for Cpf to start with 0 and cut the 0 to the left. How do I do this?

  • If you make a simple Convert.ToInt32(funcionario.cpf); doesn’t work?

  • I don’t have to work at Get

  • So where do you want to make the entire conversion?

  • That’s the question I posted. How would I do that?

  • 1

    Why do you need it, it looks like a scam and gives you trouble, but I may be wrong, you can explain it better?

1 answer

0

You need to create an employee Viewmodel with the same properties of the entity, but in this Viewmodel Cpf will be string type.

In the method GetFuncionario you need to map the Employee entity to the Employee Viewmodel, and then in the mapping you give a funcionarioViewModel.CPF = funcionario.CPF.ToString().PadLeft(11, "0"); and returns this Viewmodel to where you want.

Browser other questions tagged

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