I have 2 declared static dictionaries in Home Controller:
private static Dictionary<string, string> _existingBranchesDict;
private static Dictionary<Guid, string> _choosedBranches;
I declared them as static because they're used in many methods of the same Controller and to prevent null reference exception. The problem is they should be unique for per-user but because of static modifier these dictionaries are shared between all users. Where I have to store these data and don't have any null reference exception when I use them in different methods? I use them in different methods such as:
[HttpPost]
public void DeleteBranch(Guid partialViewID, int deletedBranchIndex)
{
if (_choosedBranches.ContainsKey(partialViewID))
{
var resettedBranch = _choosedBranches[partialViewID];
_existingBranchesDict[resettedBranch] = "Enabled";
_choosedBranches.Remove(partialViewID);
}
}
[HttpPost]
public void LoadBranch(string Name, Guid ID)
{
_existingBranchesDict[Name] = "Disabled";
if (_choosedBranches.ContainsKey(ID))
{
_existingBranchesDict[_choosedBranches[ID]] = "Enabled";
}
_choosedBranches[ID] = Name;
}
Which are called by AJAX from different partial views rendered on a single view. They are storing same data for all drop down lists for each partial view and when User choose a single element, it should be removed from other drop down lists of other partial views.