JS 客户端不调用 SignalR Hub 上的方法
JS Client doesn't invoke Method on SignalR Hub
我有一个 MVC 项目,它有 2 个按钮和 6 个文本框。我想在集线器上调用不同的方法。
客户端 :
$(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(); });
在我按下 "btnBuy" 或 "btnCell" 的情况下,它应该使用不同的参数调用服务器上的交易方法。
服务器:
BitcoinHub(名称) Class 方法
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;
}
}
在我按下 "btnBuy" 或 "btnCell" 的情况下,它会通过 clickeventhandler。问题是hub上的方法不会被调用
你不能像那样在 JS 中使用模型@Model.user。详情见Accessing MVC's model property from Javascript
另外重新安排您的代码。在建立连接之前不要在服务器上调用方法:
$(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);
});
}); });
我有一个 MVC 项目,它有 2 个按钮和 6 个文本框。我想在集线器上调用不同的方法。
客户端 :
$(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(); });
在我按下 "btnBuy" 或 "btnCell" 的情况下,它应该使用不同的参数调用服务器上的交易方法。
服务器:
BitcoinHub(名称) Class 方法
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;
}
}
在我按下 "btnBuy" 或 "btnCell" 的情况下,它会通过 clickeventhandler。问题是hub上的方法不会被调用
你不能像那样在 JS 中使用模型@Model.user。详情见Accessing MVC's model property from Javascript
另外重新安排您的代码。在建立连接之前不要在服务器上调用方法:
$(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);
});
}); });