1

I'm quite new here so if I do something wrong let me know, ok? I'm quite new in web development as well.

I'm having a problem here with a post method in ASP.NET.

Please, don't mind the name of the buttons and methods, ok? I'm Brazilian and their names are all in portuguese.

I have a submit button that calls a ng-click (Angularjs) method called AdicionarCliente().

View

     <div>
           <input type="submit" class="btn btn-info" value="Salvar" ng-click="AdicionarCliente()"/>
     </div>

JavaScript

    myApp.controller('AdicionarClientesController', function ($scope, $http)                 {
$scope.NomeCliente = "";
$scope.Telefone1Cliente = "";
$scope.AdicionarCliente = function () {
    var promisse = $http.post("/app/AdicionarCliente/", { NomeCliente: $scope.NomeCliente, Telefone1Cliente: $scope.Telefone1Cliente })

    promisse.then(function () {
                    window.location.href = "CadastroPet";
                    return false;

        });
};

It works well until this part. All the times that I hit the submit button, it comes here and enter the function in the variable "promisse".

Now - the problem is here:

Controller

     [HttpPost]
    public JsonResult AdicionarCliente(string NomeCliente, string Telefone1Cliente)
    {
        var db = new RexsoftEntities();
        db.CLIENTES.Add(new CLIENTES() { NOME = NomeCliente,
                                         TELEFONE1 = Telefone1Cliente});

            db.SaveChanges();

        var Clientes = db.CLIENTES.ToList();
       return Json(Clientes, JsonRequestBehavior.AllowGet);
    }

The first time that I hit the submit button, the code here goes until the db.CLIENTES.Add part of the code - then it doesn't run the DB.SAVECHANGES() nor the rest of the code here. The second time it works like a charm. The problems just happen on the first submit hit.

As the return of the controller doesn't happens properly, the final part of the Javascript code does not run as well. This part:

    window.location.href = "CadastroPet";
                return false;

Can anyone help me?

(All the view is inside this div

    <div ng-controller="AdicionarClientesController">

)

UPDATE

I removed the TYPE of the submit button and put the simple button type. It seems to be working now. How can I submit my form then?

Kajiyama VK
  • 336
  • 1
  • 3
  • 11
  • some sort of exception is occurring presumably when the database insert occurs. If you debug in Visual Studio does it break the execution and report an error? – ADyson Sep 15 '16 at 14:40
  • @user3127450: Could you please check the exceptions/errors if it is throwing? Please post the error logs here. So that people can answer! – Jay Patel Sep 15 '16 at 14:43
  • @ADyson It doesn't. That's the strange part. And when I check the database, the values are there - so it saved. – Kajiyama VK Sep 15 '16 at 15:35
  • so it MUST be running db.SaveChanges in that case. Your problem is somewhere else. Does your browser report any errors in the developer console when it returns from the $.post request? – ADyson Sep 15 '16 at 16:15
  • @ADyson Yes man. It IS running. That's strange. Even though I was always using the STEP OVER while running debug, it was never stopping at the SaveChanges. When I put a Break point, it stopped there. Well, but I tried to check the IE Console searching for any errors but nothing. Sometimes I get the message: "Error: SignalR: Connection must be started before data can be sent. Call .start() before .send()" - but I realised that it has nothing to do with the problem. Sometimes it appears, sometimes not. – Kajiyama VK Sep 15 '16 at 18:46
  • I Realised something - the first time I hit the submit button, the code goes to "db.CLIENTES.Add" runs it and then go back to the Javascript file, runs through it, goes back to the controller. The second time I hit the submit button it does not do this crazy trip. – Kajiyama VK Sep 15 '16 at 19:07
  • are you saying it calls the controller twice the first time? – ADyson Sep 16 '16 at 08:32
  • I removed the TYPE of the submit button and put the simple button type. It seems to be working now. How can I submit my form then? – Kajiyama VK Sep 16 '16 at 19:44

2 Answers2

0

First,as per EF best practice, try to wrap the db operation in using() { } block. Thus your controller lokks like

[HttpPost]
public JsonResult AdicionarCliente(string NomeCliente, string Telefone1Cliente)
{
    var Clientes = new CLIENTES();
    using(var db = new RexsoftEntities()) 
    {
       var _Clientes = new CLIENTES() 
       { 
           NOME = NomeCliente,
           TELEFONE1 = Telefone1Cliente
       };
       db.CLIENTES.Add(_Clientes);
       db.SaveChanges();
       Clientes = db.CLIENTES.ToList();
    }
    return Json(Clientes, JsonRequestBehavior.AllowGet);
}

Secondly, in javascript side, you are using angularjs. window.location.href will not work in angular(see this and this). You have to use $window service (source: using angularjs $window service) or $location service (source: using angularjs $location service). Also avoid using return false;.

In your case the below will work.

promisse.then(function () {
    $location.path('/CadastroPet');
});
Community
  • 1
  • 1
Adersh M
  • 596
  • 3
  • 19
  • Thx for the reply! I've got a convert problem in the line "Clientes = db.CLIENTES.ToList();" System.Data.Entity .DbSet RexsoftEntities.CLIENTES{get; set;} Cannot implicity convert type 'System.Collections.Generic.List to 'Rexsoft.Clientes' – Kajiyama VK Sep 16 '16 at 17:54
  • I removed the TYPE of the submit button and put the simple button type. It seems to be working now. How can I submit my form then? – Kajiyama VK Sep 16 '16 at 19:44
  • @user3127450 regarding your first comment: `var Clientes = new CLIENTES();` and `Clientes = db.CLIENTES.ToList();` This is trying to assign a list to a single object. Why? The ToList() command seems to be irrelevant - why return _all_ the clients? – ADyson Sep 19 '16 at 13:14
  • @user3127450 regarding your second comment: why do you want to submit your form now? You've already sent the data to the server. My understanding was once the ajax POST was successful, you redirect to another page. So doing a form submit is not relevant in that case. – ADyson Sep 19 '16 at 13:14
  • 1
    @ADyson Thx for your help, man. Yeap - about the second issue - you were right. I had to develop a workaround to validate my form and that's working now. Really - thank you very much man. – Kajiyama VK Sep 19 '16 at 17:14
0

I removed the TYPE of the submit button and put the simple button type. It seems to be working now. I created another way to validate my form using the same js script that I mentioned. If the criterias wasn't met, i would return a message and a return false statement.

Kajiyama VK
  • 336
  • 1
  • 3
  • 11