locked
ASP.NET MVC5 signalR with paging RRS feed

  • Question

  • User-1858683013 posted

    Is it possible to use <g class="gr_ gr_9 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="9" data-gr-id="9">signalR</g> with paging? I am trying to make an app and unable to get data for page no 2. <g class="gr_ gr_10 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="10" data-gr-id="10">querystring</g> gets <g class="gr_ gr_12 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins doubleReplace replaceWithoutSep" id="12" data-gr-id="12">value</g> of <g class="gr_ gr_13 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins replaceWithoutSep" id="13" data-gr-id="13">page</g> in `Hub' but <g class="gr_ gr_11 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="11" data-gr-id="11">returing</g> values are not updating on <g class="gr_ gr_14 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins replaceWithoutSep" id="14" data-gr-id="14">client</g> side. Do I need to start different Hubs for each page or different Instance? Please help me.

    My <g class="gr_ gr_8 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="8" data-gr-id="8">Client side</g> code:

    if (!String.prototype.supplant) {
        String.prototype.supplant = function (o) {
            return this.replace(/{([^{}]*)}/g,
                function (a, b) {
                    var r = o[b];
                    return typeof r === 'string' || typeof r === 'number' ? r : a;
                }
            );
        };
    }
    
    $(function () {
        var ticker = $.connection.stockTicker; // the generated client-side hub proxy
        var $stockTable = $('#stockTable');
        var $stockTableBody = $stockTable.find('tbody');
        var rowTemplate = '<tr data-rank="{currency_rank}"><td class="bold">{currency_name} ({currency_symbol})</td><td class="bold">${currency_price_usd}</td></tr>';
    
        function formatStock(stock) {
            return $.extend(stock, {
                currency_price_usd: formatprices(stock.currency_price_usd)
            });
        }
    
        function formatprices(n, sep, decimals) {
            sep = sep || "."; // Default to period as decimal separator
            decimals = decimals || 2; // Default to 2 decimals
    
            return n.toLocaleString().split(sep)[0]
            + sep
            + n.toFixed(decimals).split(sep)[1];
        }
    
        function init() {
            return ticker.server.getAllStocks().done(function (stocks) {
                $stockTableBody.empty();
                $.each(stocks, function () {
                    var stock = formatStock(this);
                    $stockTableBody.append(rowTemplate.supplant(stock));
                });
            });
        }
    
        // Add client-side hub methods that the server will call
        $.extend(ticker.client, {
            updateStockPrice: function (stock) {
                var displayStock = formatStock(stock);
                $stockTableBody.find('tr[data-rank=' + stock.currency_rank + ']').replaceWith($row);
            }
        });
    
        // Start the connection
        $.connection.hub.qs = { 'page_no': $("#page").val() };
        $.connection.hub.start().then(init);
    });

    My Hub:

    [HubName("stockTicker")]
    public class StockTickerHub : Hub
    {
        private readonly StockTicker _stockTicker;
    
        public StockTickerHub() : this(StockTicker.Instance)
        {
    
        }
    
        public StockTickerHub(StockTicker stockTicker)
        {
            _stockTicker = stockTicker;
        }
    
        public IEnumerable<CoinsViewModel> GetAllStocks()
        {
            var page = this.Context.QueryString["page_no"];
            int pageno = Convert.ToInt32(page);
            return _stockTicker.GetAllStocks(pageno);
        }
    }

    Sunday, February 11, 2018 3:39 PM

All replies

  • User61956409 posted

    Hi arbaz1295,

    Is it possible to use signalR with paging?

    Yes, it is possible. You can call and pass page number to your hub method from client side when client user click pager, and you can filter and get paged dataset based on page number and other filter condition in your hub method, then you can call client side function from hub method to push paged data to specific client user. When you do this, please avoid refreshing your page or causing postback (it would end current connection).

    You can refer to SignalR Hubs API, which show how to define client side function&hub method and how to do communication between client side and hub server.

    Besides, in my opinion, if you just want to implement paging functionality, you do not have to use SignalR. Your paged dataset might be “large”, which may cause data losing or other issues with SignalR. You can implement paging via traditional way in your backend.

    With Regards,

    Fei Han

    Monday, February 12, 2018 2:59 AM