JsonConvert System.ArgumentNullException : 'Value cannot be null. Arg_ParamName_Name'

sblb 1,166 Reputation points
2022-07-12T14:11:32.053+00:00

Hi, I tried to do the tree view structure.
The code behind is

  public string TreeViewJSON { get; set; }  
  
        public void OnGet()  
        {  
            List<TreeViewNode> nodes = new List<TreeViewNode>();  
  
            //Loop and add the Parent Nodes.  
          foreach (FilePathP type in context.FilePathPs)  
          {  
                nodes.Add(new TreeViewNode { id = type.Id.ToString(), parent = "#",text = type.Name});  
  
          }  
  
            //Loop and add the Child Nodes.  
            foreach(FilePathP subtype in context.FilePathPs)  
            {  
                nodes.Add(new TreeViewNode { id = subtype.OrgLevel.ToString() + "-" + subtype.Id.ToString() ,  
                parent = subtype.OrgLevel.ToString(), text = subtype.Name});  
            }  
  
            // Serialize to JSON string.  
             this.TreeViewJSON = JsonConvert.SerializeObject(nodes);  
  
        }  
  
        public void OnPostSubmit(string selectedItems)  
        {  
            List<TreeViewNode> items = JsonConvert.DeserializeObject<List<TreeViewNode>>(selectedItems);  
        }  

The data come from the sql view
219951-image.png

I received this message : 219855-image.png
I don't understand why I received this message because the list not null.

Have you an idea about this message and how I can solve it?

one thing that is disturbing is that I have the node counter set to 68 and it displays null values from [68] to [127]
219800-image.png

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,156 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,229 questions
{count} vote

19 answers

Sort by: Most helpful
  1. sblb 1,166 Reputation points
    2022-07-14T20:41:24.24+00:00

    I have not changed my code. (I forgot a virgule) I still get the same result as my first post.

    220903-image.png

    The value selectedItems return always a status 500. I don't know how solve it?

    t seems you simply do not have the skill set to write web applications.

    FYI I make do with my own means. I don't want to be an expert, but I need experts to help me.

    1 person found this answer helpful.

  2. sblb 1,166 Reputation points
    2022-07-15T10:52:43.29+00:00

    I've applied the structure of your example code and I've always the issue
    221111-image.png

    I can't give you selectedItems because in dev tools console I received it
    221092-image.png

    1 person found this answer helpful.

  3. Bruce (SqlWork.com) 55,366 Reputation points
    2022-07-12T15:45:44.07+00:00

    we would need the TreeViewNode class definition, and the simplest selectedItems string that causes the error.

    note: it odd you call it a TreeViewNode class and load from a hierarchical source, but create a flat list.

    0 comments No comments

  4. sblb 1,166 Reputation points
    2022-07-12T19:32:59.247+00:00

    Here the model class of TreeViewNode

     public string id { get; set; }  
            public string parent { get; set; }  
            public string text { get; set; }  
    

    I use the jQuery jsTree in HTML Razor Page.

    <div id="jstree"></div>  
                                                <form method="post">  
                                                    <input type="hidden" name="selectedItems" id="selectedItems" />  
                                                    <input type="submit" value="Submit" asp-page-handler="Submit" />  
                                                </form>  
             <script type="text/javascript">  
                                                            $(function () {  
                                                                $('#jstree').on('changed.jstree', function (e, data) {  
                                                                    var i, j;  
                                                                    var selectedItems = [];  
                                                                    for(i = 0, j = data.selected.length; i < j; i++) {      
                                                                        var id = data.selected[i];  
                                                                        if(id.indexOf('-') != -1){  
                                                                            id = id.split("-")[1];  
                                                                        }  
                                                                        selectedItems.push({  
                                                                            text: data.instance.get_node(data.selected[i]).text,  
                                                                            id: id,  
                                                                            parent: data.node.parents[0]  
                                                                        });  
                                                                    }  
                                                                $('#selectedItems').val(JSON.stringify(selectedItems));  
                                                                }).jstree({  
                                                                            "core": {  
                                                                                "themes": {  
                                                                                    "variant": "large"  
                                                                                },  
                                                                                "data": @Html.Raw(Model.TreeViewJSON)  
                                                                            },  
                                                                            "checkbox": {  
                                                                                "keep_selected_style": false  
                                                                            },  
                                                                            "plugins": ["wholerow", "checkbox"],                                                                     
                                                                });  
                                                            });  
      
    
    0 comments No comments

  5. Bruce (SqlWork.com) 55,366 Reputation points
    2022-07-13T02:03:00.99+00:00

    You should show the selectedItems postback text.