[英]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.