0

I have a MVC project which has 2 button and 6 textbox. I want to call different methods on a Hub.

Client :

$(document).ready(function () {
            var bitcoinHub = $.connection.bitcoinHub;

            $("#btnBuy").click(function () {
                var tl = document.getElementById("bitcoinBuy").value;
                var btc = document.getElementById("aBitcoin").value;
                var total = document.getElementById("aTotal").value;
                bitcoinHub.server.trade("buy",@Model.user,tl,btc,total);
            });

            bitcoinHub.client.broadcastSell = function (model) {
                //   $("#sellGrid").append("<li>"+model.Sell+"</li>");
            };

            $("#btnSell").click(function () {
                var tl = document.getElementById("bitcoinSell").value;
                var btc = document.getElementById("sBitcoin").value;
                var total = document.getElementById("sTotal").value;
                bitcoinHub.server.trade("sell",@Model.user,tl,btc,total);
            });

            bitcoinHub.client.broadcastPurchase = function (model) {
                // $("#buyGrid").append("<li>"+model.Buy+"</li>");
            };
            $.connection.hub.start();   });

In the case I press "btnBuy" or "btnCell" it should invoke the trade methode on the server with different parameters.

Server:

BitcoinHub(name) Class Method

 public void trade(string parametre, Users cUser, string tl, string btc, string total)
    {
        switch (parametre)
        {
            case "sell":
                // Do something not important here
                break;
            case "buy":
                // Do something not important here
                break;
        }
    }

In the case I press "btnBuy" or "btnCell" it goes trough the clickeventhandler. The problem is that the method on the hub will not be called.

Stephu
  • 3,236
  • 4
  • 21
  • 33
Kadir Kalkan
  • 248
  • 2
  • 10
  • I not understand our last section. Can you rewrite it that it is clearer? – Stephu Nov 17 '17 at 06:33
  • i have a mvc form. it has 2 button and 3 textbox for each button. when i press first button it will take 3 textbox' values and send to server side trade method and it will work for insert a data to database. İssue : When i press a button. Js's .click events are working together both them. When i work with breakpoint code invokes first .click code block and jumping to other button's .click function. Other İssue : but after this server side's trade method doesn't invoke. – Kadir Kalkan Nov 17 '17 at 08:08
  • can you also add your html? – Stephu Nov 17 '17 at 08:13
  • Other point: Are your shure that the connection is started? I would move the core where you subscribe click events to the $.connection.hub.start().done(function(){<--add here-->}); }); – Stephu Nov 17 '17 at 08:24
  • i am trying now but i really don't know about SignalR syntax could you fix from github if you have time ? https://github.com/kadirkalkan/PragmaLinq – Kadir Kalkan Nov 17 '17 at 08:51
  • I have no time to dive deep. What I see for short you have also a function setInterval((function () { bitcoinHub.server.createRate(); }), 3000); You can not send to server until the connection is established. Move it all to the done. Additional to that $(function () { is the same as $(document).ready( --> move together. Mybe you should take a look to https://learn.microsoft.com/en-us/aspnet/signalr/overview/guide-to-the-api/hubs-api-guide-javascript-client – Stephu Nov 17 '17 at 08:55
  • i did it but when i make it any function didn't work – Kadir Kalkan Nov 17 '17 at 09:01
  • comment added in github – Stephu Nov 17 '17 at 09:05
  • i tried it and recommented it doesn't work – Kadir Kalkan Nov 17 '17 at 09:20
  • What does not work? – Stephu Nov 17 '17 at 09:36
  • Intreval doesn't work when i cover it with hub.start.done() and my issue is when i click button it's didn't work. – Kadir Kalkan Nov 17 '17 at 09:45
  • Kakan: You are shure that the connection is established. Did you have a breakpoint there? Maybe we should switch to a chat – Stephu Nov 17 '17 at 09:46
  • facebook : https://www.facebook.com/wioper Linkedin : https://www.linkedin.com/in/kadirkalkan/ – Kadir Kalkan Nov 17 '17 at 09:50

1 Answers1

1

You can't use the model in JS like that @Model.user. See Accessing MVC's model property from Javascript for details

Additional rearrange your code. Do not invoke a method on server until connection is established:

$(document).ready(function () {
                var bitcoinHub = $.connection.bitcoinHub;           

                bitcoinHub.client.broadcastSell = function (model) {
                    //   $("#sellGrid").append("<li>"+model.Sell+"</li>");
                };

                bitcoinHub.client.broadcastPurchase = function (model) {
                    // $("#buyGrid").append("<li>"+model.Buy+"</li>");
                };
                $.connection.hub.start().done(function(){
                    // DO not call methods on server until connection is established.
                    $("#btnBuy").click(function () {
                        var tl = document.getElementById("bitcoinBuy").value;
                        var btc = document.getElementById("aBitcoin").value;
                        var total = document.getElementById("aTotal").value;
                        bitcoinHub.server.trade("buy",@Model.user,tl,btc,total);
                    });
                    $("#btnSell").click(function () {
                        var tl = document.getElementById("bitcoinSell").value;
                        var btc = document.getElementById("sBitcoin").value;
                        var total = document.getElementById("sTotal").value;
                        bitcoinHub.server.trade("sell",@Model.user,tl,btc,total);
                });
    });   });
Stephu
  • 3,236
  • 4
  • 21
  • 33