简体   繁体   English

未使用来自客户端的数据初始化字典对象

[英]Dictionary object not being initialized with data coming from client

I am facing issue my server side dictionary object is not being initialized with data coming from client. 我面临的问题是我的服务器端字典对象未使用来自客户端的数据初始化。 What is wrong with of my following code? 我的以下代码有什么问题? Do i need to mention more code here or someone can figure out based on the type of UserPermissions both on client and server side? 我是否需要在此处提及更多代码,还是有人可以根据客户端和服务器端的UserPermissions类型来确定? Is my dictionary declaration on client side wrong? 我在客户端的字典声明有误吗?

This is my client side code which push dictionary objects in array and send to server, 这是我的客户端代码,它将字典对象推入数组并发送到服务器,

$scope.studentDetail.UserPermissions = [];
var DataToBeSentToServer = { Key: Id, Value: [] };
for (var j in selectUsers) {
DataToBeSentToServer.Value.push(selectUsers[j].Id);
}
$scope.studentDetail.UserPermissions.push(DataToBeSentToServer);

Server Code: The class is being initialized outside 服务器代码:该类正在外部初始化

public class Test{
    public Dictionary<Guid, List<Guid>> UserPermissions { get; set; }
}

The JSON object you send to the server is not a dictionary. 您发送到服务器的JSON对象不是字典。 It is an object with two properties : 它是具有两个属性的对象:

public class MyJsonObject
{
    public Guid Key {get; set;}
    public Guid[] Value {get; set;}
}

By default C# dictionary is being serialized into javascript object as it basically represents a hashmap. 默认情况下,C#字典被序列化为javascript对象,因为它基本上表示一个哈希图。 That means json output from your example Dictionary<Guid, List<Guid>> will look like: 这意味着您的示例Dictionary<Guid, List<Guid>> json输出将如下所示:

{
   "85dad90f-...": [
          "413516ea-...",
          "a38473df-...",
          "bee09890-..."
   ],
   "39454a68-...": [
          "f5510938-...",
          "57260cb7-...",
          "ed201f04-..."
   ],
   "a7bc6a9e-...": [
          "77689b7c-...",
          "baf3f74b-...",
          "735f77eb-..."
   ]
}

Therefore for deserialization from js you will have to create similar data structure. 因此,要从js反序列化,您将必须创建类似的数据结构。 Note code snippet below is just to give you an idea about implementation. 下面的注释代码段只是为了让您对实现有所了解。

// assuming your data model looks like this:
var users = [{
    username : 'John Doe',
    userid : 'f5510938-...',
    permissions : [
        { permission : 'view-landing-page', permissionid : '57260cb7-...' }, ...
    ]
}, ...]


// your implementation might look like:
var permissions = {};  // to be sent to server

foreach (var userIndex in users) {
    var user = users[userIndex];
    var key = user.userid;        //  Dictionary<Guid, <- dictionary key 

    if (!permissions[key]) {
        permissions[key] = [];
    }

    foreach(pIndex in user.permissions) {
        var permission = user.permissions[pIndex];

        permissions[key].push(permission.permissionid);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在脚本中,数据来自服务器,但未初始化为变量 - In the script the data is coming from server but not initialized into variables 来自api的工具提示数据未初始化 - tooltip data coming from api not initialized 服务器数据作为客户端上的 [Object object] 传入 - Server data coming through as [Object object] on client XSS,数据来自客户端,但不保存在数据库中 - XSS with data coming from client but not saving in DB Javascript object 不存储来自 HTML 的数据 - Javascript object not storing data coming from HTML mongodb 具有 $nin 和来自客户端的数据的多个条件 - mongodb having multiple conditions for $nin and data coming from the client 使用setState中的[object Object]初始化状态 - State is being initialized with [object Object] in setState 来自后端的数据存储在对象中,但不能在对象属性中访问 - Data coming from backend is stored in object but not accessible in object properties 如何在指令链接函数angularjs中显示来自对象的数据? - How to display data coming from an object in the directive link function angularjs? 如何使用来自 json object 的数据填充“多选”? - How to populate "multiselect" with data coming from json object?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM