Send Angularjs object to Webapi C#POST

Asked

Viewed 998 times

1

I have a form, I am trying to save the information in the SQL Server database.

<html ng-app="Crm">
<body ng-controller="CrmCtrl">
<form name="novoCrmForm">
<div class="row">
    <div class="form-group text-left col-md-8">
        <label for="Nome">Nome <i class="danger">*</i></label>
        <input type="text" class="form-control" ng-model="empresaParticipante.NOME_FANTASIA" ng-required="true" name="NOME_FANTASIA" placeholder="Nome">
    </div>
    <div class="form-group text-left col-md-4">
        <label for="cpf">CPF</label>
        <input type="text" class="form-control" ng-model="empresaParticipante.CNPJ_CPF" name="CNPJ_CPF" placeholder="CPF">
    </div>
</div>
<div class="row">
    <div class="form-group text-left col-md-6">
        <label for="Telefone">Telefone</label>
        <input type="tel" class="form-control" ng-model="empresaParticipante.TELEFONE_01" name="TELEFONE_01" placeholder="Telefone">
    </div>
    <div class="form-group text-left col-md-6">
        <label for="Celular">Celular <i class="danger">*</i></label>
        <input type="text" class="form-control" ng-model="empresaParticipante.CELULAR" ng-required="true" name="CELULAR" placeholder="Celular">
    </div>
</div>
<div class="row">
    <div class="form-group text-left col-md-12">
        <label for="Email">Email <i class="danger">*</i></label>
        <input type="email" class="form-control" ng-model="empresaParticipante.EMAIL" ng-required="true" name="EMAIL" placeholder="Email">
    </div>
</div>
</form>
</div>
</div>

This form is sending the object to a function in Angularjs by the button below:

<button class="btn btn-info" data-dismiss="modal" ng-click="adicionarRegistro(empresaParticipante)" ng-disabled="novoCrmForm.$invalid">Salvar</button>

In Angularjs, when it arrives at function adicionarRegistro, I put a console.logo(empresaParticipante) to check if the object is actually arriving in this Function.

var crmApp = angular.module("Crm", []);
crmApp.controller("CrmCtrl", function ($scope, $http) {


$scope.adicionarRegistro = function (empresaParticipante) {
    $http.post("http://localhost:50183/api/crm/salvar", JSON.stringify(empresaParticipante)).success(function (data) {
        delete $scope.empresaParticipante;
        $scope.novoCrmForm.$setPristine();
        console.log(empresaParticipante);
        carregarRegistros();
    });
};

carregarRegistros();
});

So far, after checking the console, I can see the completed object.

the object:

{"NOME_FANTASIA":"bruno","CNPJ_CPF":"111.222.333-44","TELEFONE_01":"(000) 00000-0011","CELULAR":"(000) 00000-0022","EMAIL":"[email protected]"}

on the same console, under Reply, displays the following message:

{"mensagem":"Store update, insert, or delete statement affected an unexpected number of rows (0). Entities
 may have been modified or deleted since entities were loaded. See http://go.microsoft.com/fwlink/?LinkId
=472540 for information on understanding and handling optimistic concurrency exceptions.","objeto":null
,"lista":[],"status":false}

This is the class:

public partial class EMPRESAS_PARTICIPANTES
{
public int ID { get; set; }

[StringLength(150)]
public string NOME_FANTASIA { get; set; }

[StringLength(20)]
public string CNPJ_CPF { get; set; }

[StringLength(20)]
public string TELEFONE_01 { get; set; }

[StringLength(20)]
public string CELULAR { get; set; }

[StringLength(100)]
public string EMAIL { get; set; }

[Column(TypeName = "date")]
public DateTime? DATA_CADASTRO { get; set; }
}

This object should arrive populated in this API method.

[Route("salvar")]
    [HttpPost]
    public HttpResponseMessage post([FromBody] EMPRESAS_PARTICIPANTES empresaParticipante)
    {
        IEmpresas_ParticipantesBO Empresas_ParticipantesBO = new Empresas_ParticipantesBO();
        Retorno retorno = new Retorno();

        try
        {
            //salvar
            retorno = Empresas_ParticipantesBO.salvar(empresaParticipante);

How to get this object to be filled in the API and what is the reason for this message in the Reply?

  • Post the structure (properties) of the class EMPRESAS_PARTICIPANTES?

  • posted class!

  • The object empresaParticipante is null or it comes correct?

  • is coming to the null API.

  • Take that JSON.stringify when sending the object. You do not need to send a string, you need to send an object even.

  • This error is from the Entity Framework. How is the code that inserts the database record?

  • 1

    @He’s getting a null object, probably the problem has to do with it.

Show 2 more comments

1 answer

0

A possibility of the object empresaParticipante vir null is due to the header of the POST call.

Content-Type must be of the type application/json

To include Content-Type in the call:

$scope.adicionarRegistro = function (empresaParticipante) {
    var config; 

    config = {
        url : '/api/crm/salvar',
        method : 'POST',
        responseType : 'json',
        data : JSON.stringify(dataObj),
        headers : {
            'Content-Type' : 'application/json'
        }
    };


    $http(config).success(function(response) {
        console.log(JSON.stringify(empresaParticipante));
        console.log(JSON.stringify(response);       

        $scope.empresaParticipante = {};
        $scope.novoCrmForm.$setPristine();
        carregarRegistros();
    })
    .error(function(error){
        console.log(JSON.stringify(error);
    });
};

Browser other questions tagged

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