如果为空,C#ObjectMapper set list null

时间:2017-07-07 18:50:40

标签: c# asp.net-mvc treeview objectmapper

我正在使用此类作为我的权限dto:

[AutoMapFrom(typeof(Permission))]
public class PermissionDto : PermissionDto2
{
    public PermissionDto2 Parent { get; set; }


}

[AutoMapFrom(typeof(Permission))]
public class PermissionDto2
{
    public string Name { get; set; }
    public string DisplayName { get; set; }

    public string Text => Name;
    public string Value => Name;

    public bool Checked { get; set; }

    public List<PermissionDto2> Children { get; set; }

    private bool Disabled => false;
    private bool InternalDisabled => false;
}

我正在使用此功能获取我的所有权限:

    public List<PermissionDto> GetAllPermissions()
    {
        var permissions = ObjectMapper.Map<List<PermissionDto>>(_permissionManager.GetAllPermissions().Where(x => !x.Children.IsNullOrEmpty()));
        return permissions;
    } 

当我执行此函数时,它返回:

{
  "result": [
    {
      "parent": null,
      "name": "Pages",
      "displayName": "[Pages]",
      "text": "Pages",
      "value": "Pages",
      "checked": false,
      "children": [
        {
          "name": "Pages.Users",
          "displayName": "Users",
          "text": "Pages.Users",
          "value": "Pages.Users",
          "checked": false,
          "children": [
            {
              "name": "Pages.Users.Create",
              "displayName": "Users",
              "text": "Pages.Users.Create",
              "value": "Pages.Users.Create",
              "checked": false,
              "children": []
            },
            {
              "name": "Pages.Users.Edit",
              "displayName": "Users",
              "text": "Pages.Users.Edit",
              "value": "Pages.Users.Edit",
              "checked": false,
              "children": []
            }
          ]
        },
        {
          "name": "Pages.Tenants",
          "displayName": "Tenants",
          "text": "Pages.Tenants",
          "value": "Pages.Tenants",
          "checked": false,
          "children": []
        }
      ]
    },
    {
      "parent": {
        "name": "Pages",
        //...

问题在于,我用来将这些权限显示为树视图的插件ngx-treeview不允许子属性为空,就像它在权限Pages.Users.Create中一样,Pages.Users.EditPages.Tenants;所以,而不是孩子是一个空列表,我需要它为null。我怎么能这样做?

0 个答案:

没有答案