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,189 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,279 questions
{count} vote

19 answers

Sort by: Most helpful
  1. AgaveJoe 26,136 Reputation points
    2022-07-14T13:00:55.85+00:00

    I gave the result ot the TreeViewJSON. Why you insist that I didn't provide the value?

    Um yeah, I don't see the image anywhere in this post. Secondly, the screenshot is NOT the value of selectedItems parameter! It is a C# object from the Visual Studio debugger. The value of selectedItems is found in the browser's dev tools. It's the payload in the network view and a string!

    220746-image.png

    So the question is why the selectedItems is null in my configuration? Have you an idea?

    Because your code has bugs. I'm guessing you copied the code from the Internet and have no idea how the code works. The fact that you cannot provide the value of selectedItems parameter shows that you do understand the fundamentals.

    0 comments No comments

  2. sblb 1,166 Reputation points
    2022-07-14T15:49:52.437+00:00

    The only way to see the hidden value is to use dev tools in console :

    220818-image.png
    I don't know what else to do

    The status is 500. Have you an idea how I can solve it?

    and have no idea how the code works.

    you often refer to our friend google!


  3. sblb 1,166 Reputation points
    2022-07-15T14:04:15.14+00:00

    I agree with you I obtained a null value for selectedItems (see third post)

    221125-image.png

    I tried to do the same structure of you but there is an error because it doen't take into account foreach()

       public void OnGet()  
            {      
                        Nodes = new List<TreeViewNode>()  
                        {  
      
                              foreach (FilePathP genre in context.FilePathPs)  
                               {  
                                new TreeViewNode { id = genre.Rows.ToString(),  
                                                                parent = "#",  
                                                               text = genre.Name });  
      
                              }  
      
           
                        foreach (FilePathP subtype in context.FilePathPs)  
                        {  
                            new TreeViewNode { id = subtype.OrgLevel.ToString() + "-" + subtype.Rows.ToString(),  
                                                             parent = subtype.OrgLevel.ToString(),  
                                                             text = subtype.Name });  
                        }  
                    }  
            }  
    

    221162-image.png

    There is a problem of the synthax due to the foreach loop. it won't solve the problem but I would like to try with your structure

    0 comments No comments

  4. AgaveJoe 26,136 Reputation points
    2022-07-15T15:22:33.75+00:00

    I tried to do the same structure of you but there is an error because it doen't take into account foreach(). There is a problem of the synthax due to the foreach loop. it won't solve the problem but I would like to try with your structure

    You misunderstand my code sample. The sample is not a solution, it is test code. The intent is to illustrate a POST where the selectedItems parameter has the expected JSON string format.

    You latest screenshot shows the selectedItems is empty. That means your code has bugs. Why is the JavaScript not populating the hidden field? Is the change event firing? Or perhaps the design bug is coming from the server code?

    I can tell you with confidence that your server side code makes no logical sense. It loops over the same FilePathPs collection twice. On the second loop the logic concatenates OrgLevel with Id separated by an "-". Well, the Id is a GUID and GUIDs always contains a "-". So your JavaScript code, if the event fires, will break the GUID.

    With all that being said, your design has too many fundamental problems to understand the actual intent.

    What I find very confusing is the jstree can display a directory structure. The jstree site even has an example. Why you are still messing with the jQuery DataTable is a mystery.

    0 comments No comments

  5. sblb 1,166 Reputation points
    2022-07-15T15:42:59.82+00:00

    The sample is not a solution, it is test code

    Ok I understood !!!!

    well, the Id is a GUID and GUIDs always contains a "-"

    I added the columns to add an identifier.
    221201-image.png

    It loops over the same FilePathPs collection twice. On the second loop the logic concatenates OrgLevel with Id separated by an "-".

    Do you have a proposal?

    I reproduced my same issue with your structure.
    In this case I returned any selectedItem. So I think there a problem with js script.

            [BindProperty]  
            public List<TreeViewNode>? Nodes { get; set; }  
    
            public void OnGet()  
             {  
                 Nodes = new List<TreeViewNode>()  
                 {  
                     new TreeViewNode()  
                     {  
                         id = "1",  
                         parent = "",  
                         text = "Root"  
                     },  
                     new TreeViewNode()  
                     {  
                         id = "2",  
                         parent = "Root",  
                         text = "Folder1"  
                     },  
                 };  
             }  
    
             public void OnPost(string selectedItems)  
             {  
                 Nodes = System.Text.Json.JsonSerializer.Deserialize<List<TreeViewNode>>(selectedItems);             
             }  
    

    Js Script

        <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++) {  
    
                                                                //Fetch the Id.  
                                                                var id = data.selected[i];  
    
                                                                //Remove the ParentId.  
                                                                if (id.indexOf('-') != -1) {  
                                                                    id = id.split("-")[1];  
                                                                }  
    
                                                                //Add the Node to the JSON Array.  
                                                                selectedItems.push({  
                                                                    text: data.instance.get_node(data.selected[i]).text,  
                                                                    id: id,  
                                                                    parent: data.node.parents[0]  
                                                                });  
                                                            }  
                                                        }).jstree({  
                                                            "core": {  
                                                                "themes": {  
                                                                    "variant": "large"  
                                                                },  
                                                                "data": {  
                                                                         var json = @Html.Raw(System.Text.Json.JsonSerializer.Serialize(Model.Nodes));  
                                                                         document.getElementById("selectedItems").value = JSON.stringify(json);  
                                                                         console.log(json);  
                                                                          }  
                                                                    },  
                                                                    "checkbox": {  
                                                                        "keep_selected_style": false  
                                                                    },  
                                                                    "plugins": ["wholerow", "checkbox"],  
                                                                 });  
                                                    });  
                                            </script>