Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Store values in static dictionary

I have this code , and i want to store values of parameters left, top in static dictionary. and after storing this values i want to access them in Jquery.Thanks For your Help.

my code

using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Hubs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebApplication1.MoveShape
{
    public class MoveShapeHub : Hub
    {
        public void calculate(string left, string top)
        {
            Clients.Others.updateshape(left, top);

        }

    }
}
like image 410
Jot Dhaliwal Avatar asked Nov 02 '22 09:11

Jot Dhaliwal


1 Answers

jQuery operates client-side, so you can't talk to it directly. You have two options, then:

  • request the value via http (perhaps using SignalR as a messaging layer, since you are referencing that)
  • serialize the dictionary as JSON into the request

Fortunately, the latter is fairly trivial for most common types - here using Json.NET:

var data = new Dictionary<string, object>
{
    {"foo", 123},
    {"bar", "abc"},
    {"blap", true}
};
var json = JsonConvert.SerializeObject(data);

which gives us the JSON:

{"foo":123,"bar":"abc","blap":true}

If you assign that into a variable in the <script>, then values can be referenced either as obj.bar or as obj['bar']. However: keep in mind that all values will be serialized if you do this - you may want to be more restrictive in terms of tracking which the client actually cares about (or should be allowed to know about).


Important point, though: if your "static dictionary" is actually a static dictionary, then please consider very carefully the impact of multiple users. static is a really good way to write a web-site that only scales to a single user.

like image 57
Marc Gravell Avatar answered Nov 12 '22 22:11

Marc Gravell