Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I have List of Drives in DropDownList and wanted to display list of Directories by selecting any drive.

List Drive Names:

HTML
var drives = Directory.GetLogicalDrives().ToList();
            ViewBag.Drives = new SelectList(drives);


Only Last Alphabetical Folder/Directory is shown even the method returns all.

Please help.

What I have tried:

Method in Controller to List Directories:
HTML
public JsonResult GetDirectories(string drive)
       {
           var d = new List<string>();
           foreach (var directories in new DirectoryInfo(drive).GetDirectories())
           {
               d.Add(directories.Name);
           }
           var dr = new SelectList(d.ToList());
           return Json(dr, JsonRequestBehavior.AllowGet);
       }


JQuery in View:

HTML
<script type="text/javascript">
    $(document).ready(function(){
        $("#Drives").change(function () {
            var d = $("#Drives").val();
            $.ajax({
                type: "post",
                url: "/Explorer/GetDirectories?drive=" + d,
                data: { drives: $("#Drives").val() },
                data: "json",
                traditional: true,
                success: function (data) {
                    var folders = '';
                    for (var i = 0; i < data.length; i++)
                    {
                        folders = folders + '<li class="list-group-item"> <a href="" class = "fa fa-folder">'+data[i].Text+'</a> </li>';
                    }
                    $('#folders').html(folders); 
                }
            });
        });
    });

</script>
Posted
Updated 31-Jul-20 8:38am
v2
Comments
ZurdoDev 30-Jul-20 15:35pm    
Debug it and find out what is going on.
nyt1972 31-Jul-20 6:01am    
don't know how to debug JQuery code?
ZurdoDev 31-Jul-20 7:18am    
Press F12 in your browser to open the developer tools. Then, depending on browser, there's probably a tab for source which is where you will see the javascript code. You can then put breakpoints and step through the code.
nyt1972 31-Jul-20 14:39pm    
Thanks a lot, it was logical error in JQuery script, I corrected and updated my Question too.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900