Send object from AngularJs to POST of WebAPI C #

2

I have a form, which 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 Angles 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 in function adicionarRegistro , I put console.logo(empresaParticipante) to check if the object is actually arriving at 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]"}

In the same console, in Response, 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 get 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 make this object get populated in the API and what is the reason for this message displayed in the Reply?

    
asked by anonymous 01.07.2016 / 15:12

1 answer

0

One possibility that the object empresaParticipante null is due to the header of the POST call.

The Content-Type must be of type application/json

To include Content-Type to 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);
    });
};
    
02.07.2016 / 00:50