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.

13
  • I not understand our last section. Can you rewrite it that it is clearer? Commented Nov 17, 2017 at 6: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. Commented Nov 17, 2017 at 8:08
  • can you also add your html? Commented Nov 17, 2017 at 8: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-->}); }); Commented Nov 17, 2017 at 8:24
  • i am trying now but i really don't know about SignalR syntax could you fix from github if you have time ? github.com/kadirkalkan/PragmaLinq Commented Nov 17, 2017 at 8:51

1 Answer 1

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); }); }); }); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.